CSE 1321 - Module 1 A Programming Primer 10/4/2019 CSE 1321 Module 1
Ps Skeletons BEGIN MAIN END MAIN Note: every time you BEGIN something, you must END it! Write them as pairs! 10/4/2019 CSE 1321 Module 1
Skeletons #include <iostream> int main() { std::cout << "Hello World!\n"; } Note: Capitalization matters! 10/4/2019 CSE 1321 Module 1
Skeletons #2 – Same thing! #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; } Note: The “namespace” thing keeps you from having to type “std” all the time! 10/4/2019 CSE 1321 Module 1
Ps Printing BEGIN MAIN PRINT “Hello, World!” PRINT “Bob” + “ was” + “ here” END MAIN Ps 10/4/2019 CSE 1321 Module 1
Printing #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; cout << "Bob" << " was" << " here!" << endl; } 10/4/2019 CSE 1321 Module 1
Declaring/Assigning Variables BEGIN MAIN CREATE userName CREATE studentGPA userName ← “Bob” studentGPA ← 1.2 END MAIN userName “Bob” studentGPA 1.2 Ps 10/4/2019 CSE 1321 Module 1
Declaring/Assigning Variables #include <iostream> #include <string> using namespace std; int main() { string username; float gpa; username = "Bob"; gpa = 1.2f; } 10/4/2019 CSE 1321 Module 1
Reading Text from the User BEGIN MAIN CREATE userInput PRINT “Please enter your name” READ userInput PRINT “Hello, ” + userInput END MAIN Ps 10/4/2019 CSE 1321 Module 1
Reading Text from the User #include <iostream> #include <string> using namespace std; int main() { string userInput; cout << "Please enter your name: "; getline (cin, userInput); cout << "Hello, " << userInput << "!" << endl; } 10/4/2019 CSE 1321 Module 1
Reading Numbers from the User BEGIN MAIN CREATE userInput PRINT “Please enter your age: ” READ userInput PRINT “You are ” + userInput + “ years old.” END MAIN Ps 10/4/2019 CSE 1321 Module 1
Reading Numbers from the User #include <iostream> using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << "You are " << age << " years old." << endl; } 10/4/2019 CSE 1321 Module 1
Note There are times when reading strings and numbers immediately after one another is problematic. We’ll talk about that later. 10/4/2019 CSE 1321 Module 4