Sign in to follow this  
Daerus

How would I loop this? (newbie question)

Recommended Posts

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!

Share this post


Link to post
Share on other sites

#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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Check out the Iteration Structures (loops) section at this link. It'll surely help you with this problem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Gah... here's the link

http://www.cplusplus.com/doc/tutorial/control.html

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
What exactly does that for(;;) mean? I've never seen it used that way with the ';;'. Would that be different than for()?

Thanks for the additional help :)

Share this post


Link to post
Share on other sites
for(;;) is equivalent to while(true), that is it's an infinite loop. You can leave any of the three for fields blank if you don't need them (the second field defaults to true), e.g.:


int g_iLoopCt = 0;

for (; g_iLoopCt < 8; ++g_iLoopCt)
{
/// ...blah
}

Share this post


Link to post
Share on other sites
for(;;) repeats all functions inside its '{' '}' brackets again and again until broken. Like while() only it repeats without stop unless it reaches a 'break;'command. for instance:


for(;;)
{
cout << "Text";
}

The above would repeat 'Text' 'Text' 'Text' endlessly.

And the below:


for(;;)
{
cout << "Input number: ";
cin >> int num;
if(num == 7)
{
break;
}
}

Would keep asking for a number until a seven is entered, at which point it will 'break' and cut through the loops.

I am sure there are better ways to do things, but I am still learning c++ myself.

Share this post


Link to post
Share on other sites
Quote:

for(;;)
{
cout << "Input number: ";
cin >> int num;
if(num == 7)
{
break;
}
}


Like socrates200X stated:

int iDummy = int();

for( ; iDummy != 7 ; )
{
std::cout << "Input Number: ";
std::cin >> iDummy;
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this