Thread: not looping after if()

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    not looping after if()

    I received a chain email (arghhh) recently and I thought I would try and make a program out of this particular one for practice.

    The point of the program is to figure out the pattern for a few math riddles and and use the pattern to answer the final math question.

    Code:
    #include <stdio.h>
    
    int main()
    {
              int user_input, loop = 1; 
              char rept;
              {  
              printf("\nTry the following!\n");
              printf("\nIf:\n");
              printf("3 + 5 = 24\n");
              printf("5 + 4 = 45\n");
              printf("6 + 8 = 84\n");
              
              printf("\nThen:\n");
              }
              do  
              { 
                {
                printf("\n9 + 7 = ");
                loop++;
                scanf("%d", &user_input);
                
                loop = user_input;
                
                printf("\nWrong! Try Again? (y)es or (n)o: ");
                rept = getchar();
                getchar();
                }              
                if((rept == 'y') || (rept == 'Y'))
                {
                         continue;
                }
                else
                {
                         break;
                }
              }
              while(loop != 144);
              {
                         printf("\nCorrect!\n");
              }
              return 0;
    }
    The problem is I'm not sure what statement to use if the user selected y or Y.

    Code:
                if((rept == 'y') || (rept == 'Y'))
                {
                         continue;
                }
                else
                {
                         break;
                }
              }
    continue; just ends the loop and prints Correct!

    I have been advised against using continue; and break; by my lecturer (what are my options?).

    I was also told that break; is like force quitting the program, is that true?
    Last edited by SilentPirate007; 03-07-2010 at 03:54 PM. Reason: Forgot some stuff...

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The break; statement breaks out of the loop dropping through to the next statement after the
    Code:
    while () {}
    loop ends

    This block of code is what you want:

    Code:
    if (rept != 'y' && rept != 'Y')
      break;
    But if your professor doesn't want you to use break; or continue then you have to put the condition in the while loop test condition:
    Code:
    unsigned int yn = 'y';
    while ((yn == 'Y' || yn == 'y') && answer != number) {
    ....
        printf("\nWrong! Try Again? (y)es or (n)o: ");
        yn = getchar();
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Ok, but the problem still remains that if the user wants to try again, it exits the loop.

    The code does, however, need some more fixing, because at the moment even the right answer is wrong when running the program.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: not looping after if()

    Try this code, it will solve your requirement.

    Code:
    #include <stdio.h>
    
    int main()
    {
            int user_input, loop = 1;
            char rept;
            {
                    printf("\nTry the following!\n");
                    printf("\nIf:\n");
                    printf("3 + 5 = 24\n");
                    printf("5 + 4 = 45\n");
                    printf("6 + 8 = 84\n");
    
                    printf("\nThen:\n");
            }
            do
            {
                    printf("\n9 + 7 = ");
                    scanf("%d", &user_input);
    
                    loop = user_input;
    
                    if(loop == 144 )
                            break;
    
                    printf("\nWrong! Try Again? (y)es or (n)o: ");
    
                    /* Here you made mistake */
                    getchar(); // here newline will be omitted
                    rept = getchar(); 
                    
    
            } while((rept == 'y') || (rept == 'Y'));
    
            if(loop == 144)
                    printf("\nCorrect!\n");
            return 0;
    }
    And some of the changes I made in your code.
    Or you can check and print inside the loop itself.

    Code:
    if(loop == 144)
    {
    printf("Correct\n");
    break;
    }
    If you used this, There is no need to check and print outside the loop.
    Last edited by sganesh; 03-08-2010 at 12:27 AM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66

    Thumbs up Break

    I was also told that break; is like force quitting the program, is that true?
    Nope, Break is used to come out from the loop , it is used to exit from the loop not from the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM