Jump to content
  • Advertisement
Sign in to follow this  
Daerus

How would I loop this? (newbie question)

This topic is 4489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
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
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!