Thread: Basic C menu

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    7

    Question Basic C menu

    Hi im kinda new to C and im learning from a teach yourself c book, but i dont really understand how to make a basic menu. Basically i want to make a program which can display all of my little programs which i have been working on throughout the book. I would be grateful if you could give me a webpage or some code. I dont have any code, as i said earlier i have no idea what to do! Thanks for looking

    Smilk

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can do it kinda like this:

    -printf the options to the screen.
    -Get user input (normally a number)
    -Use a switch to branch to the relevant section of code.

    Try having a go at a few basics (like just printf'ing the menu to start with), the post your code here when you have problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Talking

    Try This...

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int choice=0;
    while(choice!=4)
    {
     clrscr();
     printf("\n\tMENU DEMONSTRATION");
     printf("\n\t------------------------------");
     printf("\n\n\t 1. OPTION 1");
     printf("\n\t 2. OPTION 2");
     printf("\n\t 3. OPTION 3");
     printf("\n\t 4. EXIT");
     printf("\n\n Enter Your CHoice: ");
     scanf("%1[1234]d%*c",&choice);
     switch(choice)
     {
      case 1:
                  printf("\nYOU SELECTED OPTION 1 %c",1);
                  break;
      case 2:
                  printf("\nYOU SELECTED OPTION 1 %c",2);
                  break;
      case 3:
                  printf("\nYOU SELECTED OPTION 1 %c",3);
                  break;
      case 4:
                  printf("\nYOU SELECTED OPTION 1 %c",4);
                  exit(0);
      otherwise:
                 printf("\nINVALID SELECTION...Please try again");
      }
      getch();
    }
    }
    -Sriharsha
    Help everyone you can

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%1[1234]d%*c",&choice);
    And what happens if the user enters an invalid option after a valid one that doesn't end the loop? Say perhaps on the first iteration you enter 1, but the second you enter 7. This also won't work as you think because the [...] replaces the d format code, so you're actually reading 1 character that only accepts 1, 2, 3, or 4. So the switch will always print the default case unless you check for char literals instead of int literals.

    >while(choice!=4)
    This is an infinite loop, since you are entering chars and testing for ints this condition will never be met unless you know and can enter the EOT value in ASCII or the PF value in EBCDIC.

    >otherwise:
    I believe you mean default:.

    >return 0;
    This line is missing.

    Here's another way to do it. The program ignores multiple digit numbers when it should flag an error, but this will suffice for an example.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    int main ( void )
    {
      int choice=0;
      while(choice!='4')
      {
        clrscr();
        printf("\n\tMENU DEMONSTRATION");
        printf("\n\t------------------------------");
        printf("\n\n\t 1. OPTION 1");
        printf("\n\t 2. OPTION 2");
        printf("\n\t 3. OPTION 3");
        printf("\n\t 4. EXIT");
        printf("\n\n Enter Your Choice: ");
        choice = getche();
        switch(choice)
        {
        case '1':
          printf("\n\nYOU SELECTED OPTION 1\n");
          break;
        case '2':
          printf("\n\nYOU SELECTED OPTION 2\n");
          break;
        case '3':
          printf("\n\nYOU SELECTED OPTION 3\n");
          break;
        case '4':
          printf("\n\nYOU SELECTED OPTION 4\n");
          break;
        default:
          printf("\n\nINVALID SELECTION...Please try again\n");
        }
        (void)getch();
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Smilk, did any of these replies confuse you? Did you receive enough help?
    The world is waiting. I must leave you now.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    16

    Interesting Way

    Code:
    #include <stdio.h>
    
    int menu(int MenuChoice);
    
    int main()
    {
    int WhereInMenu;
    char KeyTouched;
    
    WhereInMenu = 1;
    
    menu(1);
    
    for(;;)
    	{
    		KeyTouched = getch();
    		if (KeyTouched == 80)
    		{
    			WhereInMenu++;
    			if (WhereInMenu > 4)
    			{
    				WhereInMenu = 1;
    			}
    			menu(WhereInMenu);
    		}
    
    		else if (KeyTouched == 72)
    		{
    			WhereInMenu--;
    			if (WhereInMenu < 1)
    			{
    				WhereInMenu = 4;
    			}
    			menu(WhereInMenu);
    		}
    		else if (KeyTouched == 27)
    		{
    			system ("cls");
    			printf("Press Enter To Exit...");
                getch();
    			return 0;
    		}
    	}
    }
    
    
     
    int menu(int MenuChoice)
    {
    	switch (MenuChoice)
    		{
    		case 1:
    		system ("cls");
    		printf("\n* Option Number 1");
    		printf("\n  Option Number 2");
    		printf("\n  Option Number 3");
    		printf("\n  Option Number 4");
    		break;
    
    		case 2:
    		system ("cls");
      		printf("\n  Option Number 1");
    		printf("\n* Option Number 2");
    		printf("\n  Option Number 3");
    		printf("\n  Option Number 4");
    		break;
    
    		case 3:
    		system ("cls");
    		printf("\n  Option Number 1");
    		printf("\n  Option Number 2");
    		printf("\n* Option Number 3");
    		printf("\n  Option Number 4");
    		break;
    
    		case 4:
    		system ("cls");
    		printf("\n  Option Number 1");
    		printf("\n  Option Number 2");
    		printf("\n  Option Number 3");
    		printf("\n* Option Number 4");
    		break;
    
    		default:
    		system ("cls");
            printf("\nError");
    		}
    	return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you may be 'pr0ficient', the initial poster was not. As such, your code will only confuse them.

    For example:

    If they don't know how to do simple menuing, there is great likelyhood that a blank 'for' loop will only confuse them.

    Additionally, you use screen clearing, which is non ANSI code. It's best when learning programming to stick with only the ANSI standard so you're sure your code will actually work where the poster is compiling.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    Hi thanks everyone for trying to help me, obviously i found some of it pretty confusing, but still it has helped me loads, thanks alot.

  9. #9
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    Talking simple is best

    Code:
    #include <stdio.h>   //for printf and scanf satements
    int main()
    {
       int choice;
    
       /*Displaing on screen*/
       printf("-------Menu-------\n");
       printf("1) Program 1\n");
       printf("2) Program 2\n");
       printf("3) Program 3\n");
    
       /*getting input*/
       scanf("%d" &choice);
    
       system("CLS");
    
       /*Finding which choice was asked for (my style of using brackets may be different than yours*/
       if (choice==1)
       {
          printf("You chose program 1!\n");
       }
       else if (choice==2)
       {
          printf("You chose program 2!\n");
       }
       else if (choice==3)
       {
          printf("You chose program 3!\n");
       }
    
       system("PAUSE");
       return 0;
    }
    if u need more help putting your program together dont be afraid to e-mailing me:

    [email protected]
    Last edited by red_baron; 05-11-2002 at 05:37 PM.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by red_baron
    >scanf("%d" &choice);
    You didn't do any error checking here. What if a letter is entered by mistake? And you forgot the comma, so it won't compile for me anyway.

    >system("CLS");
    Read quzah's post. Here's a quote:
    >Additionally, you use screen clearing, which is non ANSI code.
    (The same goes for the system("PAUSE") call.)

    Also, system() lives in stdlib.h, which you haven't included.

    I think smilk has had enough confusion for one day
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    16
    How was I able to compile my menu without including stdlib then?

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    How was I able to compile my menu without including stdlib then?
    By some strange miracle...

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    He wasn't even talking to you.

    : : edit : :
    > Originally posted by red_baron
    :P
    Last edited by Shadow; 05-11-2002 at 11:05 PM.
    The world is waiting. I must leave you now.

  14. #14
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Hey,,
    I think this ammendment should work with out problems...

    [code]
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    char choice='0';
    while(choice!='4')
    {

    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\tMENU DEMONSTRATION");
    printf("\n\t---------------------------------------------");
    printf("\n\n\t 1. OPTION 1");
    printf("\n\t 2. OPTION 2");
    printf("\n\t 3. OPTION 3");
    printf("\n\t 4. EXIT");
    printf("\n\n Enter Your CHoice: ");
    scanf("%1[1234]c%*c",&choice);
    switch(choice)
    {
    case '1':
    printf("\nYOU SELECTED OPTION 1 %c",1);
    break;
    case '2':
    printf("\nYOU SELECTED OPTION 1 %c",2);
    break;
    case '3':
    printf("\nYOU SELECTED OPTION 1 %c",3);
    break;
    case '4':
    printf("\nYOU SELECTED OPTION 1 %c",4);
    exit(0);
    default:
    printf("\nINVALID SELECTION...Please try again");
    }
    getch();
    }
    return 0;
    }


    - Or do any problems still exist..

    -Sriharsha
    Help everyone you can

  15. #15
    Registered User
    Join Date
    May 2002
    Posts
    14
    How was I able to compile my menu without including stdlib then?
    the compiler will assume whatever function you dont include a header for is extern and search the standard headers then headers in the directory your working from for the matching function
    PHP Code:
    int main()
        {
            
    printf("testing...one...two...cheeze!");
        } 
    this works great without stdio.h but it still warns me
    warning C4013: 'printf' undefined; assuming extern returning int
    i dont get how this works can someone explain?
    scanf("%1[1234]c%*c",&choice);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM