while/ do while loop

Pages: 12
I'm given this simple program

#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?
I'll give you some hints.
char again; - Needs default value ;)

while (again == 'y' || again ++ 'Y') 2 Errors on This line

1
2
cin >> again;
return 0;

Error between these lines.

Last edited on
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>
using namespace 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
Last edited on
That aside. There is no validate on the inputs.

He should use something like:
1
2
3
4
5
6
string input = "";

cout << "Enter Number:";
cin.getline(input);
// Do checks to make sure input is number
// continue... 
@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.
Sorry, I edited it =P
You need to edit it again. There is a bug in your code.

But as he has stated, his tutor said a do..while is not the solution.
whoops, the ++ was meant to be == at the beginning.
and theres a second error? I'm looking in my book and the examples look just like that...

With char again, can't I leave it as is if I'm using 'y' and 'Y' as the values?

And I didn't know there was a problem with the last two lines. I'm assuming it's not a syntax error as my compiler isn't recognizing it.


And I understand the difference between do while and the while loop. The do while loop would make this alot easier...
Last edited on
The 2nd problem with the while loop relates to the last error.

And if you follow my hint for the default value that should fix the app.

And I understand the difference between do while and the while loop. The do while loop would make this alot easier...


Actually it wouldn't. It'd be the same just 2 lines swapped around.
Last edited on
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
Last edited on
Yes, you found the errors! (I left those two bugs in on purpose, Zaita)

Now, look at this:
1
2
3
char again;

while (again == 'y' || again == 'Y')

Looking at that, you have no guarantee that again is y or Y. So setting it to y or Y ensures that you enter the loop.
Hence, "char again = 'y';"
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?
Last edited on
What your saying is

1
2
3
4
5
char again = 'y'; // make again == y

while (again == 'y') {
 // only do this IF again is 'y' and until again is NOT 'y'
}


Note: if again is never changed during the loop to a value that is not 'y' then it's considered an infinite loop and your program will never end.
Last edited on
I changed the errors you stated and it seems to be working as intended

#include <iostream>
using namespace std;
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;
}


It just seems like putting it in do while form is more easily read.
Last edited on
Foe wrote:
so whenevr I put again to equal y or Y, it will reset the program to the first statement after the condition?

Exactly ;)

Otherwise, it just goes right out of the loop, to return 0;, which ends the program.

And yes, your code looks good :)
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
Last edited on
As a matter of preference. Formatting your code is equally as important as getting it right. Pick a standard coding style and stick to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace 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;
}


Notice use of whitespace etc and indentation.
@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?

Last edited on
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 :)
So how can I convert it to use only numbers using that code?(not an assignment, just interested)
Last edited on
Pages: 12