# Thread: Program to determine sum

1. Originally Posted by jadedreality
Okay I've changed it around a little, it works but not like it's supposed to...when I press 1 for Yes it exits instead of letting me try again.

Code:
```#include    <iostream>
#include    <ostream>
#include    <istream>

int main()
{
char  y;

int  a;
int  b;
int  c;

std::cout
<< "This program will display the sum of three numbers." << std::endl;

while (1 == Y);	{

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;

}

return 0;

}```
When read 1 as a character it is represented by the character '1', which is distinct from the numeral 1. So you should check if '1' == Y.

2. >> while (1 == Y);

Ok, you're closer to the syntax, but still not quite right. There's something there that shouldn't be there.

Also, as King Mir mentioned, if the variable Y is a char, then compare it against '1'. If the variable Y is an int, like how avgprogamerjoe used, then you can compare it against 1.

3. 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.

Popular pages Recent additions