Thread: Option problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    23

    Option problem

    Hello, I'm new to this forum and programming in general,but I'd like to learn,and I already started with C programming.

    But in a program I'm recently writing I have a problem.It's a small program,nothing fancy for someone with my level and experience.But I wanted to give the user three options like this:

    1. Option
    2. Option
    3. Option

    And I wondered if you guys can help me out with a code to make any other inputs into the program other then these three options illegal,with a message to please try again from the following options.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Use switch ,

    Code:
    #include<stdio.h>
    
    main()
    {
            int op;
            printf("Please Enter the option ( 1 / 2 / 3 ) : ");
            scanf("%d",&op);
            switch(op)
            {
                    case 1:
                    printf( "You have entered one \n" );
                    break;
                    case 2:
                    printf( "You have entered two \n" );
                    break;
                    case 3:
                    printf( "You have entered three \n" );
                    break;
                    default:
                    printf( "Invalid option\n" );
            }
    }

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    23
    Thank you.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    23
    The code is almost what I'm looking for,only with two things missing.

    How do you make the program stop to display the results,because getchar() doesn't seem to work,other then system("pause"),and I read it's not a good idea to use system calls.

    And the other thing is,how could I make the program return to the original options displayed the first time if you hit the wrong key and type "try again"?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    How do you make the program stop to display the results,because getchar() doesn't seem to work,other then system("pause"),and I read it's not a good idea to use system calls.
    You can either run your program for the console / command-line (where your program will exit, but the output will still be visible) or use a different function at the end. stdio.h provides several functions that require hitting enter before moving on. None of them are completely ideal for that purpose, but at least they're standardized. In the real world, non-GUI programs usually just exit, as they are usually run from the console anyway (or they're intended to just disappear once they're done).

    Having said that about 'real-world' apps, there's nothing THAT wrong with using system("pause") as long as it's just part of a learning exercise, or a program for fun. Just keep in mind that your code won't be portable on other systems, and you might be missing out on a better way to do things.

    And the other thing is,how could I make the program return to the original options displayed the first time if you hit the wrong key and type "try again"?
    Have you learnt about loops yet? A do / while loop would be best. You put your code in the "do" block, and then repeat it while the input is invalid. As soon as they enter something valid, the loop exits.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    New York
    Posts
    11
    you may use getch() instead of getchar(), getch() is included in conio.h

    example:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    { 
     printf("Press something.\n");
     getch();
     printf("Done something.\n");
     getch();
    
     return 0;
    }
    karthigayan, if you dont mind, i will use your code in this example.
    you should use a WHILE loop in here, here is an example:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
            int op;
            printf("Please Enter the option ( 1 / 2 / 3 ) : ");
             
            while(op != 1 && op != 2 && op != 3)
            {        
            scanf("%d",&op);
    
            switch(op)
            {
                    case 1:
                    printf( "You have entered one \n" );
                    break;
                    case 2:
                    printf( "You have entered two \n" );
                    break;
                    case 3:
                    printf( "You have entered three \n" );
                    break;
                    default:
                    printf( "Try again\n" );
            }
          }
    }
    if this seems confusing, the != sign means if 'not equal'. so if OP is
    not 1 nor 2 or 3, then that means the user typed an incorrect number
    so the loop keeps going. however, if OP is equal to either 1, 2, or 3
    then the loop exits. simple, eh?

    Cheers, ComputerWarrior.
    Last edited by ComputerWarrior; 08-02-2010 at 10:24 AM.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by ComputerWarrior View Post
    you may use getch() instead of getchar(), getch() is included in conio.h

    example:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    { 
     printf("Press something.\n");
     getch();
     printf("Done something.\n");
     getch();
    
     return 0;
    }
    conio.h and getch() are not part of the "standard" language as defined in the C99 standard. Some implementations (compilers and libraries) support them.

    [Edit: Removed comment about now corrected while line.]
    Last edited by pheininger; 08-02-2010 at 01:27 PM. Reason: Original code corrected.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    New York
    Posts
    11
    sorry 'bout that, i was in a hurry so i must have forgotten the && signs...
    thanks for pointing that out pheininger!

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    23
    Thanks guys,I'm interested in one more thing.I saw on certain programs that you can "refresh" the window from the text,in order to show the next available text or options.Can you guys tell me what code it is?

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I saw on certain programs that you can "refresh" the window from the text,in order to show the next available text or options.
    I'm not sure what you mean here. You can call fflush() on output streams, which ensures that all text you have asked to be outputted is actually outputted. It's useful for debugging when your program crashes after an output statement before the output is actually performed.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    23
    It's something to do with what system clearscreen does,but I was wondering if you could advise of a better way of doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fred fwrite
    By alexrelph in forum C Programming
    Replies: 2
    Last Post: 03-07-2010, 05:45 PM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM