Here we show how to perform simple input, and some computation.
#include <iostream>
/*
* This program reads two numbers from the console and prints the total.
*/
int main()
{
// The two input values.
int x,y;
// Request and read input.
std::cout << "Enter an integer: ";
std::cin >> x;
std::cout << "Enter another integer: ";
std::cin >> y;
// Print the total, with proper labels.
std::cout << "The sum of " << x << " and " << y
<< " is " << x + y << "." << std::endl;
}
New stuff here:
- The declaration of two integer variables will be familiar
from Java.
- The >> is used to read values.
- Notice that the prompts do not use endl. Lets the
user type right after the prompt.
- Note how the line of ouput is
built from multiple variables and constants.
[more info]