Printing output according to letter chosen problem

This is a discussion on Printing output according to letter chosen problem within the C Programming forums, part of the General Programming Boards category; Instead of using 1,2,3 I want the user to select from A,B,C but I'm having some problem finding the right ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Printing output according to letter chosen problem

    Instead of using 1,2,3 I want the user to select from A,B,C but I'm having some problem finding the right solution. I have tried 'A' or '65' which is A in ASCII but I have no luck. How can I get the user to select a letter and then display the appropriate output?

    Code:
    #include <stdio.h>       //Handles the keyboard input and screen output
    #include <process.h>   //Handles DOS system commands ("cls", "pause")
    
    void(main)
    {
       //This part declares variables used by the program
       int reply=0;        //Users reply
       int i, items = 'C';   //Loop counter, loop limit
       //This part displays the menu items
       system("cls");
       printf("Welcome to MENU program!");
       while (reply < items)
       {
            printf("\n\nHere are menu options that this program offers:");
            //This part displays menu options
            for (i = 'A'; i < items; i++)
            {
                    printf ("\n%d. Option number %d", i, i);
            }
            printf ("\n%d. (or %d, ..., etc.) EXIT program.", items, (items+1));
            printf ("\n");      // Changes the line
            printf("\nTo terminate choose the option %d, or higher!", items);
            printf("\nPlease choose the item to run (e.g. 1, ..., %d) ", items);
            scanf("%d", &reply);  // Inputs your reply
            printf("Your choice was option number %d", reply); //Option
            //Completes the processing for subsequent options
            if (reply == 'A')
            {
                    //Here complete actions for menu option number 1
                    printf ("\nOption number 1 is executed here!");
            }
            if (reply == 'B')
            {
                    //Here complete actions for menu option number 2
                    printf ("\nOption number 2 is executed here!");
            }
            // ... (continue with options)
            if (reply >= items)
            {
                    //This completes actions for the option PROGRAM EXIT
                    printf (" which terminates this program!");
            }
            if (reply < 'A')
            {
               //Here is the "wrong reply" error message
               printf ("\nChoice %d is out of range (retype 1, ..., %d).",
                       reply, items);
            }
       }
    
       //The final displays
       printf ("\n\nThank you for using this program. Good bye!");
       printf ("\n");           // Changes the line
       system("pause");    // DOS "Pause" command (for user to look at results)
    }

  2. #2
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Guti14
    Instead of using 1,2,3 I want the user to select from A,B,C but I'm having some problem finding the right solution. I have tried 'A' or '65' which is A in ASCII but I have no luck. How can I get the user to select a letter and then display the appropriate output?

    Code:
            scanf("%d", &reply);  // Inputs your reply
    'reply' should be an int, and you should should use "%c".

    But you should not use scanf() that is a difficult function to use correctly.

    For many reasons (read the FAQ), better to use an fgets() based function.
    Code:
    <...>
       {
          char s[3];
    
          fgets (s, sizeof s, stdin);
    
          reply = *s;
       }
    Last edited by Emmanuel Delaha; 08-12-2004 at 03:21 AM. Reason: typo
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    One more thing
    Code:
    void(main)
    should be
    Code:
    int main(void)
    {
    /*your code */
    return 0;
    }
    Woop?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Cheers guys, but neither advice has gotten me closer to completing the program I think.

  5. #5
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Guti14
    Instead of using 1,2,3 I want the user to select from A,B,C but I'm having some problem finding the right solution. I have tried 'A' or '65' which is A in ASCII but I have no luck. How can I get the user to select a letter and then display the appropriate output?
    Use "%c" with printf() too.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    why not a switch statement?
    Code:
    char reply = '\0';
     
     while(reply=='\0')
     {
     	... //accept input here
     
     	switch(reply)
     	case 'a':
     		... //if 'a' do stuff
     		break;
     
     	case 'b':
     		... //if 'b' do stuff
     		break;
     
     	case 'c':
     		... //if 'c' do stuff
     		break;
     
     	default:
     		reply = '\0'; //if input is not a,b or c then ask for input again
     		break;
     }
    Last edited by sand_man; 08-12-2004 at 04:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing output
    By TAboy24 in forum C Programming
    Replies: 2
    Last Post: 12-07-2007, 12:41 AM
  2. problem printing out structure after loading
    By pari in forum C Programming
    Replies: 17
    Last Post: 11-23-2002, 09:12 PM
  3. Printing Problem
    By RubenJ in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2001, 12:27 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Visual C++ printing console output
    By clarinetster in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2001, 01:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21