Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Returning Back To The Beginning Of An If Statement

Recommended Posts

I have a bit of code. I am sooo close to figuring out. The only thing I need it to do, is return back to the beginning of the if statement after the new input is entered to determine of the new input meets the requirements. Here is my code:

Code:

int input() //request input from user{	int size = 0, size_even = 0;	cout << " Please enter an odd number between 3 and 25: ";	cin >> size;   	size_even = size % 2;    	// if statement to determine if the number is between 3 and 25	if((size >= 3) && (size <= 25))	{  // nested if statement to determine if the number is even or odd  if(size_even == 0)  {  	cout << " Invalid Entry! Please enter an ODD number between 3 and 25! ";  	cin >> size;  }  else  	cout << " This is an odd number. ";	}	else  cout << " Invalid Entry! Please enter a number BETWEEN 3 and 25!";  cin >> size;	return 0;}


So for instance as it is now, if some one enters a number larger than 25, it prompts for a new number but that's it. I need it to prompt for the new number and then go back through the if statemtents to determine if it meets the conditions.

Thanks for anyhelp provided.

kvarnerexpress

Share this post


Link to post
Share on other sites

Um, is this your homework?!

 

Look up control structures in your book.

 

You'll learn a lot more if you figure it out yourself. Once you read the section on control structures the answer will be clear.

 

If you are still stuck you should probably email your prof instead of this forum. Your prof will be a better resource in the long run.

 

good luck

Share this post


Link to post
Share on other sites

let me help him cheat

int input() //request input from user{    int size = 0, size_even = 0;    bool bPass = false;    do    {        cout << " Please enter an odd number between 3 and 25: ";        cin >> size;                     // if statement to determine if the number is between 3 and 25        if((size >= 3) && (size <= 25))        {              size_even = size % 2;             // nested if statement to determine if the number is even or odd             if(size_even == 0)            {                   cout << " Invalid Entry! Please enter an ODD number between 3 to 25! ";            }            else            {                   cout << " This is an odd number. ";                   bPass = true;            }        }        else               cout << " Invalid Entry! Please enter a number BETWEEN 3 and 25!";    }while(!bPass)    return 0;}

Share this post


Link to post
Share on other sites

I would do something like:

...do {   cout << " Please enter an odd number between 3 and 25: ";   cin >> size;             } while( size < 3 || size > 25 );...

If this is your homework, I would suggest you do not ask for other people to solve it for you. You won't be able to post a message and wait for a reply on a forum during an exam.

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  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.