Thread: Adding statement between loops

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

    Adding statement between loops

    If you have a look at the following code:

    Code:
    #include <stdio.h>
    
    int main()
    {
              int answer;//users answer input
              
             {
               printf("\nIf:");
               printf("\n2 + 3 = 10\n");
               printf("7 + 2 = 63\n");
               printf("6 + 5 = 66\n");
               printf("8 + 4 = 96\n");
              }
              do
              {
               printf("\nThen:\n");
               printf("\n9 + 7 = ");
               scanf("%d", &answer);
              }
              while(answer != 144);//only loops if the answer is anything but 144
              return 0;  
    }
    If you compile and run the program, it would look like this:

    If:
    2 + 3 = 10
    7 + 2 = 63
    6 + 5 = 66
    8 + 4 = 96

    Then:

    9 + 7 = 27// red only implies its wrong in this example for demonstration purposes'.

    Then:

    9 + 7 = 144//The program will now end

    What I really want is that the program give the statement "Try again y/n?" so the user can opt to try again. Example:

    If:
    2 + 3 = 10
    7 + 2 = 63
    6 + 5 = 66
    8 + 4 = 96

    Then:

    9 + 7 = 23

    Wrong! Try Again "y" or "n"? //if the answer is y, then the program will loop the same way until the user either types n or gets it right.
    Last edited by SilentPirate007; 02-28-2010 at 01:44 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Well you need to adjust the structure of your
    Code:
     do { } while
    loop to fringe on the Y/N answer, not the answer == 144. Which also means you will need to add a
    Code:
    if (answer == 144) break;
    statement if you want it to also terminate on the answer equaling 144, or you could also include that in the while condition but that would be a bit messy.

    Reading in Y/N answer properly is beyond this response but I'll tell you using
    Code:
    scanf("%c",&some_char)
    is definitely
    not a good solution.

    You should use fgets and an appropriate buffer, this is because you are likely to leave user input in the buffer. Which means it will likely pass that to the next scanf call instead of getting new user input. Even with fgets you could still easily leave user input in as you are not making an unbuffered call so the user is free to type pages of data when you only want a single Y/N input heh.
    Last edited by nonpuz; 02-28-2010 at 04:32 PM. Reason: grammar mistakes/removed code tags

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up

    Is this you want ,

    Code:
    #include <stdio.h>
    
    int main()
    {
            int answer;//users answer input
            char opt[2];
    
            {
                    printf("\nIf:");
                    printf("\n2 + 3 = 10\n");
                    printf("7 + 2 = 63\n");
                    printf("6 + 5 = 66\n");
                    printf("8 + 4 = 96\n");
            }
            while(1)
            {
                    printf("\nThen:\n");
                    printf("\n9 + 7 = ");
                    scanf("%d", &answer);
                    if(answer==144)
                    {
                            printf("It's right\n");
                            break;
                    }
                    else
                    {
                            printf("Wrong ! Try again ( Y(y) / N(n)) : ");
    again:
                            scanf("%s",opt);
                            if(strcmp(opt,"Y")==0||strcmp(opt,"y")==0 )
                                    continue;
                            else if(strcmp(opt,"N")==0 ||strcmp(opt,"n")==0)
                                    break;
                            else
                                    printf("Invalid option (Y(y) / N(n))");
                            goto again;
    
                    }
            }
    }
    Thanks
    Last edited by karthigayan; 02-28-2010 at 11:27 PM. Reason: to align the code

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't hand out solutions! Neither you nor anyone else will benefit for it!
    Furthermore, you should work on your own code: do NOT use goto!
    All can be solved with a little logic. Write a flowchart, then write out the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Dont use scanf.
    Code:
    do
              {
               printf("\nThen:\n");
               printf("\n9 + 7 = ");
               scanf("%d", &answer);
              }
              while(answer != 144);
    If your input is a letter, this will end up being a infinite loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  2. loops in a main statement....
    By xero18 in forum C Programming
    Replies: 8
    Last Post: 10-30-2005, 09:36 PM
  3. If statement....
    By TheProphecy in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2005, 02:24 PM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. For/Next Loops...adding 10 numbers...
    By IanelarAzure in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2002, 12:02 PM