CSE 1321 Module 1 A Programming Primer Skeletons













- Slides: 13
CSE 1321 - Module 1 A Programming Primer
Skeletons BEGIN MAIN END MAIN Note: every time you BEGIN something, you must END it! Write them as pairs! Ps
Skeletons #include <iostream> int main() { std: : cout << "Hello World!n"; } Note: Capitalization matters!
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!
Printing BEGIN MAIN PRINTLINE “Hello, World!” PRINT “Bob” + “ was” + “ here” END MAIN Ps
Printing #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; cout << "Bob" << " was" << " here!"; }
Declaring/Assigning Variables user. Name BEGIN MAIN CREATE user. Name “Bob” CREATE student. GPA user. Name = “Bob” student. GPA = 1. 2 END MAIN student. GPA 1. 2 Ps
Declaring/Assigning Variables #include <iostream> #include <string> using namespace std; int main() { string username; float gpa; username = "Bob"; gpa = 1. 2 f; }
Reading Text from the User BEGIN MAIN CREATE user. Input PRINT “Please enter your name” READ user. Input PRINT “Hello, ” + user. Input END MAIN Ps
Reading Text from the User #include <iostream> #include <string> using namespace std; int main() { string user. Input; cout << "Please enter your name: "; getline (cin, user. Input); cout << "Hello, " << user. Input << "!" << endl; }
Reading Numbers from the User BEGIN MAIN CREATE user. Input PRINT “Please enter your age: ” READ user. Input PRINT “You are ” + user. Input + “ years old. ” END MAIN Ps
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; }
Note There are times when reading strings and numbers immediately after one another is problematic. We’ll talk about that later.