Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14COSC 181 – Foundations of Computer ProgrammingClass 3Last TimeBasic C++ Programs (Output/Input)“escape sequences”Basic main() functionstd::cout and std::cin<< and >>// commentsTaking input from keyboard and adding integerssystem(“PAUSE”);Today’s GoalsFinish from FridayMore basic vocabularyVariables and Computer MemoryPractice with “arithmetic operators”arithmetic precedencePractice with “relational operators”overall operator precedenceIntroduction to Software Engineering processWhat does this program do then?// Text-printing program 2.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Welcome "; std::cout<< " to C++!\n”; system(“PAUSE”); // prepackaged wait function return 0; // indicate that program ended successfully} // end function mainOUTPUT:Welcome to C++!What about this program?// Text-printing program 3.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Wel\ncome\nto\n\nC+\n+!\n”; system(“PAUSE”); // prepackaged wait function return 0; // indicate that program ended successfully} // end function mainOUTPUT:WelcometoC++!Getting input from the keyboardstd::cinStandard character input streamInput in C++ occurs over streams as wellThe standard stream (above) is connected to the keyboard by defaultnamespace :: command“>>” (as opposed to “<<“) OperatorStream extraction operatorVariable DeclarationsVariables are used to store certain dataThe input data for our next program will be stored in variablesMust first be declaredint number1; (type variable_name)int number2;int sum;Then they can be initializedsum = number1 + number2;std::cin>>number1;Using Input/Addition/Output#include <iostream> // allows program to perform input and output// function main begins program executionint main(){ // variable declarations int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2 std::cout << "Enter first integer: "; // prompt user for data std::cin >> number1; // read first integer from user into number1 std::cout << "Enter second integer: "; // prompt user for data std::cin >> number2; // read second integer from user into number2 sum = number1 + number2; // add the numbers; store result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line system(“PAUSE”); return 0; // indicate that program ended successfully} // end function mainNow Try it YourselfOpen up DevC++Type in the CodeUnder the “Execute” menu First hit “Compile”Wait for the screen to say Done (0 errors, 0 warnings)Then hit “Run”Vocabularyfundamental (primitive) typesint, char, doubleidentifiera.k.a. – variableIdentifiers can be anything so long as they aren’t keywords and don’t begin with a digitint return;int pq34_34;int 1number;Make sure your variable names make senseC++ is “case-sensitive”“char myVariable;” is different from “char myvariable;” Only this one is allowable.Memory ConceptsEvery variable is linked to a spot in memoryEach variable has a name, size, type and value.Writing to a memory location (setting a variable) is “destructive”Reading from a memory location (outputting a variable) is “nondestructive”Look at Figs 2.6, 2.7, and 2.8 in your bookEquality and Relational OperatorsFig 2.12 gives a list of these operators><>=<=== (not to be confused with assignment operator “=“)!=binary operatorsEach gives a value of either “true” or “false” (1 or 0)Each is evaluated from left to rightbool answer = 343 > 43;bool answer = 23 <= 3;Code from Fig 2.13A few new tricks are presented here–“using”using std::cout;using std::cin;using std::endl;1. cin >> number1 >> number2;2. if ( number1 = = number2) cout << number1 << “==“ << number2 << endl;Try It YourselfType in the code as we did before•Compile and Run•Enter 3 Test Cases1. 99 982. 5 1003. 23
View Full Document