Jul 25, 2008

Returning Back To The Beginning Of An If Statement

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Returning Back To The Beginning Of An If Statement

kvarnerexpress
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:

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

 

 

 


Reply

sandbox
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

Reply

Poe
let me help him cheat

CODE

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


 

 

 


Reply

Spectre
I would do something like:

CODE
...
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.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for returning, back, beginning, statement
advertisement



Returning Back To The Beginning Of An If Statement



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE