How would I loop this? (newbie question)

Started by
12 comments, last by raz0r 18 years, 1 month ago
So I have this code, pretty simple and self explanatory:

#include <iostream>
using namespace std;

void main()
{
	int num;
	
	cout << "Please enter a number between 1 and 10: " << endl;
	cin >> num;

	if ( (num < 1) || (num > 10) )
	{
		cout << "That is not a correct value." << endl;
	}

	else
	{
		cout << "You entered: " << num << endl;
	}
}

But now.. what I want to do, is if they enter a value that's greater than 10 or less than 1, I want it to go back to the main() function and start again. I've tried doing this with a while loop, it will just keep looping, and if I break the while loop, then it goes to the end. Anyone have any hints? Thanks!
Advertisement
#include <iostream>using namespace std;void main(){	int num;		cout << "Please enter a number between 1 and 10: " << endl;	cin >> num;	while( (num < 1) || (num > 10) )	{		cout << "That is not a correct value." << endl;		cout << "Please enter a number between 1 and 10: " << endl;		cin >> num;	}	cout << "You entered: " << num << endl;}


something like that perhaps?
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Check out the Iteration Structures (loops) section at this link. It'll surely help you with this problem.
You can do it with a while loop, just break it when they get it right.
while(TRUE){//get number and if right break, else is going to repeat}
TG Ramsus
Gah... here's the link

http://www.cplusplus.com/doc/tutorial/control.html
Excellent, thanks for the help!

I was on the right track with the while loop, I just didn't have them input a character, so the loop would keep looping if they got it wrong.

I'm going to check out that tutorial, and also try out that while(true).

Thanks!
People beat me to it, but heres my code:
#include <iostream>using namespace std;int main(){    for(;;)    {	   	    int num;		    cout << "Please enter a number between 1 and 10: " << endl;	    cin >> num;	    while(num >= 1 && num <= 10)	    {            if ( (num < 1) || (num > 10) )            {		        cout << "That is not a correct value." << endl;		    }		    else	    	{                cout << "You entered: " << num << endl;	    	}	    	system("PAUSE");        }    }    return 0;}


It has you input a number and then repeats it if in between 1-10 or asks for a new number if more than ten or less than one.
Hmm... You can also do a do loop:
... stuff ...int number;do {    cout << endl << "Enter a number: ";    cin >> number;} while(number < 1 || number > 10);


A do loop ALWAYS loops ATLEAST once, no matter if the first time it loops the condition is false:
do {    // this will always happen ONCE} while (false);


So, if it's ok it just breaks and you have a number... If it isn't, it loops again...
Quote:Original post by Servant of the Lord
People beat me to it, but heres my code:
*** Source Snippet Removed ***

It has you input a number and then repeats it if in between 1-10 or asks for a new number if more than ten or less than one.


May I ask why you have a big for loop in there?
Quote:Original post by agi_shi
May I ask why you have a big for loop in there?


Seems obvious; perhaps theres a better way though. The for(;;) loop keeps repeating so if you put it a number that is not 1-10 the while breaks and it asks for a new number, which is what I though the OP wanted. Is there something wrong with a for(;;) loop? Admittingly your code is better, but that is my code that I came up with when the poster asked, and it works.

This topic is closed to new replies.

Advertisement