Code:
#include <iostream>
//#include <ostream> Only Wikipedia will tell you to include these two
//#include <istream>
int main()
{
char y = '1'; //initialize this variable, so that when you check it later, it has meaning.
int a, b,c;
std::cout
<< "This program will display the sum of three numbers." << std::endl;
while (y == '1') { //the character constant '1', which is true the first time, since we set y to '1', remember? Otherwise this loop wouldn't run at all.
std::cout << "Enter an integer: ";
std::cin >> a;
std::cout << "Enter an integer: ";
std::cin >> b;
std::cout << "Enter an integer: ";
std::cin >> c;
std::cout << "The sum is " << a + b + c << "." << std::endl;
std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;
std::cin >> y; //You have to take input, otherwise how do we know what the user wanted?
}
return 0;
}
I suggest you follow some more tutorials. There are many on the internet, and the more you read the better of a grasp you'll have over what the code you're trying is actually doing.
As for this program, I might make a few minor design changes.
- Call y something like go_on, and have it take the character values 'y' or 'n'.
- Use a do-while loop in the place of your while loop (look it up). It will improve readability and eliminate the mysterious initialization to 'y' or '1' at the beginning of your program.
Happy programming.