the1whoknocks
the1whoknocks

Reputation: 89

Return to the beginning of an if statement

I was wondering if there was any way to return to the beginning of an if statement when the user enters something we do not want him to enter. For example:

#include <stdio.h>

int main()
{
    int number1;

    printf("Please enter a number:"); scanf("%d", &number1);

    if(blablabla)
        printf("blabla");
    else if(blablabla)
        printf("blabla");
    else if(blablabla)
        printf("blabla");
    else
        /*I want to return to the beginning of this if statement so the user can try again*/
}

I saw some things on goto, but I am not sure it is the way to go... Thank you very much! :]

Upvotes: 2

Views: 14967

Answers (5)

TruthSeeker
TruthSeeker

Reputation: 1579

int main()
{
    int number1;
    int loop_state = 0;

 do{   
    printf("Please enter a number:"); scanf("%d", &number1);
      switch(number1)
         {
          case 1: break;
          case 2: break;
          default: loot_state = 1 ;/*User value is not as expected*/
          }
   }while(loop_state==1);

return 0;
}

Upvotes: 0

Ian Hazzard
Ian Hazzard

Reputation: 7770

I think that you want to reset the main() function when the user enters something invalid. If so, try this:

function main(){

if(blablabla){

printf("blabla");
return false; /* If the code hits a "return false", 
the code stops running and resets the function. */

}

if(blablabla){

printf("blabla");
return false;

}

if(blablabla){

printf("blabla");
return false;

}

return true; // If the code does not "return false" then "return true".

}

Let me know if that helps.

Upvotes: 0

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Put it all inside a while loop, and only exit the loop if the input is good.

while (true) {
    printf("Please enter a number:"); 
    scanf("%d", &number1);
    if (blablabla) {
        printf("blabla");
        break; //good and break
    } else if (blablabla) {
        printf("blabla");
        break; //good and break
    } else if(blablabla) {
        printf("blabla");
        break; //good and break
    }
    // no good and stays
}

Upvotes: 1

Grice
Grice

Reputation: 1375

Try a while statement:

int valueIWantItToBe = 1;//Whatever value you want
do {
    printf("Please enter a number:"); scanf("%d", &number1);
} while (number1 != valueIWantItToBe);

Upvotes: 0

Don Shankin
Don Shankin

Reputation: 440

You can restructure a little to use a do-while loop. I cant get to gcc at the moment, but try something along the lines of:

#include <stdio.h>

int main()
{
    int number1;

    bool continueLooping = true;    
    do 
    {
        printf("Please enter a number:"); scanf("%d", &number1);
        if (successful input 1)
        {
            /* perform some action */
            continueLooping = false;
        }
        else if (successful input 2)
        {
            continueLooping = false;
        }
        else
        {
            /* Failed input */
        }

    } while (true == continueLooping);
}

Edit: some details

basically, you want to execute infinitely until you get proper input. So, change focus to always act on "good" input. Run through your test cases on the input, and for each successful case, you want to chose to exit the loop.

In the case of bad input, don't change the loop condition, so you run through the entire process again

Upvotes: 0

Related Questions