Hi guys, I have just written 2 versions of this simple program:

#include <iostream>
using std::cin;
using std::cout;

int main ()
{
cout << "This program counts from 10 to 0. \nGuess the missing number.\n";
int n;
int f;
for (n=10; n>0; n--)
	{
	if (n==5) continue;
	cout << n << ", ";
	}
cout << "So, type the missing number here: ";
cin >>f;
	if (f==5)
	cout << "Well done!\a\a\a";
	while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
	
return 0;
}

I compiled it and executed it but it doesn' work properly: if I guess the number on the first attempt, all good, but, say that at the first attempt I try with 4 (and the program will obviously say tht that's wrong asking me to try again) and then I type 5, the program terminates, without showing me "Well done" and ringing the system alarm three times.
Now in the second version I changed the order of the if statement and the while loop so that line 17 and 18 come after the while loop:

#include <iostream>
using std::cin;
using std::cout;

int main ()
{
cout << "This program counts from 10 to 0. \nGuess the missing number.\n";
int n;
int f;
for (n=10; n>0; n--)
	{
	if (n==5) continue;
	cout << n << ", ";
	}
cout << "So, type the missing number here: ";
cin >>f;
	
	while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
	if (f==5)
	cout << "Well done!\a\a\a";
return 0;
}

Now, this one here works fine. What I would like you guys to help me with, is to understand why.
thanks

Recommended Answers

All 7 Replies

The problem is that while loop. When f is not 5, that sequence will continue to loop but as soon as f equals 5 then it will continue on to the next line which is not the "well done".

Hi there,
thanks for that. uhm, I thought the next line was the "well done" bit because I have the loop and then the if statement so, if f is not 5 the loop will execute, if f is 5 then the loop won't execute and it will jump straight to the "well done" line. Is it not like that?
thanks

cout << "So, type the missing number here: ";
cin >>f;
	if (f==5)
	cout << "Well done!\a\a\a";
	while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
    return 0;
}

Look more closely at this piece of code. If you enter 5, as you say it works fine. But if you enter 4 the first thing the code does is:

cout << "So, type the missing number here: ";
cin >>f;
	if (f==5)
	cout << "Well done!\a\a\a";

Nothing prints because f != 5
Then the program continues and executes this code:

while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
    return 0;
}

You enter the loop and keep asking for another number until f==5
What happens when you exit the loop? What's missing?

It is because each line executes in order. The order you have here is to check if f == 5 first, and if so then cout<< your message. If f!=5 though, it will check to see if f==5 once, then go into the loop. There is no code to take you back to the check to see if f==5 and cout<< your message after you exit the loop (when f==5).

if (f==5)	cout << "Well done!\a\a\a";	while (f!=5)	{	cout << "Wrong answer, try again!\nEnter the number here: ";	cin >> f;	}

This one keeps asking for input as long as f!=5. Then, whenever f==5, it will exit the loop and go on to your if statement to check the value of f.

cin >>f;
	
	while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
	if (f==5)
	cout << "Well done!\a\a\a";

edit: Oops, Walt, you beat me to it. I finally found a question I can answer...so I'll leave my answer here even though it's the same thing you said.

ok, so fondamentally, what you guys are saying is that the if statement has to be after the loop, whic makes sense, because we need to tell exit the loop if f=5.
I presume this thing of having the f statement after the loop is always valid when they are used together

Yes, depending on what you want to do. But in this case you really don't even need the if statement, since the loop will only stop whenever f==5. So we already know that it's 5, and don't need to check.

I would think that this logic is always valid whenever you have a while loop that ends when a certain value is reached and then you want to check for that value after exiting the loop.

But if you want to check for a certain value before entering the while loop, then you'd put the if statement first.

Ok so I can simply remove the line that goes

#
if (f==5)

and nothing at all will change.
Let me get to this though:

But if you want to check for a certain value before entering the while loop, then you'd put the if statement first.

Any chance you can give me a practical simple example?Just so I understand it properly
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.