#include <iostream>
using namespace std;
main()
{ int num1, num2;
char again;
while (again == 'y' || again ++ 'Y')
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
return 0;
}
I'm supposed to fix any errors that are in the code and the only error I can find is that the while loop needs to be a do while loop. Well, my instructor has told me that it needs to stay in the form of a while loop, and I cannot figure out how to run this program in the while loop and make it work as intended. What can I do to make the while loop work as a do while?
To solve your problem, there are two ways. The first is as you said, to use a do while.
Here's how:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
main()
{
int num1, num2;
char again;
do
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
while (again == 'y' || again ++ 'Y');
return 0;
}
Note the semicolon after the while statement!
Alternatively, you can change the line "char again;" to "char again = 'y'".
The while loop as you have it right now checks the condition in the parentheses as soon as you reach the beginning. So the condition must be true for the loop to run at all. On the other hand, a do while loop will ALWAYS run once, and only check the condition to see if it should run again. Both ways will work in this case!
EDIT: I'll let you use Zaita's hints to figure out the errors =P
@Timaster: Please don't post full solutions to people's homework. This teaches them to just ask, and we get a huge influx of people who just simply post "please do this for me". This forum is about HELPING people, not doing it for them.
I'm assuming in need a second set of { after the condition and before return 0} ? to make the loop have a block, but the whole program not responding because it's a while loop is my main concern.
Edit: really? alright, I'll try the value, one sec
ahah, I didn't think making again = y would cause it to work correctly. I was assuming that when I got to the end of the code that it would just end. I guess I don completely understand the while loop
EDIT: so whenevr I put again to equal y or Y, it will reset the program to the first statement after the condition?
I just had it in my head that the while loop would only work with things before it(as it's called a pre test loop) Didn't know I could use it on the post code.
Oops yeah, I didn't notice that missing int in front of main()
And my code was somewhat formatted, it just didn't survive the copy paste :P
#include <iostream>
usingnamespace std;
int main() { // Error here, main should always be int main()
int num1, num2;
char again = 'y';
while (again == 'y' || again == 'Y') {
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
}
return 0;
}
@Zaita
What's wrong with just using cin? In the code as it was, the loop would go on if again is y or Y, and in all other cases stop. That is excactly the thing you want again to do. So why use your code above?
using cin >> var; opens up a lot of problems as there is no type-checking done and you end up having to use extra function calls like cin.ignore('\n'); to clear the buffer.
By using
1 2
string input = "";
cin.getline(input);
You take the whole line and the \n is dropped. From there you can convert it to the format you wish in the desired way and put proper checks and exception handling around your code. This will help prevent errors generated from user input. Idiot Proof you app :)