#include using namespace std; // This is a sample program illustrating basics we've seen so far int main() { char ch; // declare variables int value1, value2, x; // this allocates memory // prompt for input - always nice to let the user know // what you expect cout << "Please enter a character followed by 2 integers. " << endl; // the double quotes tells the computer I want the enclosed text // string written to the screen as is // the << symbol is called the insertion operator // we are inserting information into the output stream // the endl tells the computer to start a new line. It is only used // with output. It's like pressing the enter key while you're typing // try removing it along with its << insertion operator is see what // happens // now we need to read in the data the user will enter at the keyboard // each variable you wish to read information into is preceeded by // an extraction operator >> cin >> ch >> value1 >> value2; // we could also have written this using 2 or 3 cin statements // try it out! // the user can enter the data with 1 or more spaces between items // they could also use tabs, carriage returns between items // the computer expects the user to enter a character 1st followed // by 2 integers because of the order they are written in the // cin statement // Let's redisplay the data in our 3 variables cout << endl << "you entered the character " << ch << ", and the integers " << value1 <<" and " << value2 << endl; // notice that there are no quotes around the variable names in a cout // statement. this tells the computer to print the contents of the // variable - not its name. If we put quotes around the names we would // be telling it to print out the names // placing another character into ch ch = 'w'; cout << "ch now has charcter " << ch << endl; // lets sum value1 and value2 cout << " the sum of " << value1 << " and " << value2 << " is: " << value1 + value2 << endl; // another way we could have handled the sum x = value1 + value2; // sum them and place into another variable // this is good to do if you are going to use // the calculated value again later in the program cout << " the sum of " << value1 << " and " << value2 << " is: " << x << endl; // you'll see that the screen result looks the same to the user // in general, if we do need a calculation more than for 1 output // it's a good idea to save the result in a variable for later use. // That way you don't have to recalculate to get the result again. return 0; }