Thread: How to make a menu on C? DOS

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    How to make a menu on C? DOS

    Hello,

    Can somebody explain me how to make a menu in C?
    The menu must call another functions and validate that I enter a valid key, when I hit enter in the other screen it must go back to the main menu

    For example.
    MAIN MENU
    1. Sum
    2. Rest
    Press function to continue.

    If I press 1. The whole screen must be blank and show me something like
    Enter digit number 1:
    Enter digit number 2:
    The result is :

    Press Enter to go the main menu.


    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    You can try this

    Code:
    int choice;
    
    do 
    {
     printf("Menu\n\n");
     printf("1. Sum\n");
     printf("2. Rest\n");
     printf("3. Exit\n");
     scanf("%d",&choice);
      
     switch (choice)
     {
         1: /*Call function here to do the required operation*/
              break;
         2: /*Call function here to do the required operation*/
              break;
         3: printf("Goodbye\n"); 
             break;
         default: printf("Wrong Choice. Enter again\n");
                        break;
     } 
     
    } while (choice != 3);

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    It didnt work.

    Code:
    It seems to be easy but I get many errors.
    Building main.obj.
    C:\Archivos de programa\PellesC\Projects\3\main.c(5): warning #2027: Missing prototype for 'sum'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(10): error #2048: Undeclared identifier 'f12'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(13): error #2168: Operands of + have illegal types 'char *' and 'float'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(13): error #2140: Type error in argument 1 to 'printf'; found 'float' expected 'restrict const char *'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(21): warning #2027: Missing prototype for 'printmenu'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(26): warning #2027: Missing prototype for 'printmenu'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(26): error #2120: Redeclaration of 'printmenu' previously declared at C:\Archivos de programa\PellesC\Projects\3\main.c(21): found 'void __cdecl function(void)' expected 'int __cdecl function'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(38): error #2001: Syntax error; found ':' expecting ';'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(38): error #2061: Illegal statement termination.
    C:\Archivos de programa\PellesC\Projects\3\main.c(39): warning #2154: Unreachable code.
    C:\Archivos de programa\PellesC\Projects\3\main.c(41): error #2001: Syntax error; found ':' expecting ';'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(41): error #2061: Illegal statement termination.
    C:\Archivos de programa\PellesC\Projects\3\main.c(43): error #2001: Syntax error; found ':' expecting ';'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(43): error #2061: Illegal statement termination.
    C:\Archivos de programa\PellesC\Projects\3\main.c(44): warning #2154: Unreachable code.
    *** Error code: 1 ***
    Done.
    
    
    #include <stdio.h>
    
    
    void sum()
    {
    	float f1,f2;
    	printf("Enter first number\n\n");
    	 scanf("%f",&f1);
    	printf("Enter second  number\n\n");
    	scanf("%f",&f12);
    	float total;
    	total = f1 + f2;
    	printf("The result is" + f1 +  f2);
    	printf("Press key to go to the main menu\n\n");
    
    	// Here it must detect the enter key and then go to the main menu
    }
    
    void main()
    {
    	printmenu();
    }
    
    
    void printmenu()
    {
    		int choice;
    	do 
    	{
    		 printf("Menu\n\n");
    		 printf("1. Sum\n");
    		 printf("2. Rest\n");
    		 printf("3. Exit\n");
    		 scanf("%d",&choice);
      
    		 switch (choice)
     		{
    			1:
      		 	sum();
              		break;
         			2: /*Call function here to do the required operation*/
          			break;
         			3: 
    			printf("Goodbye\n"); 
             		break;
        			default: printf("Wrong Choice. Enter again\n");
                        break;
     		}	 
     
    	} while (choice != 3);
    	
    }
    
    
    
    <code>
    #include <stdio.h>
    
    
    void sum()
    {
    	float f1,f2;
    	printf("Enter first number\n\n");
    	 scanf("%f",&f1);
    	printf("Enter second  number\n\n");
    	scanf("%f",&f12);
    	float total;
    	total = f1 + f2;
    	printf("The result is" + f1 +  f2);
    	printf("Press key to go to the main menu\n\n");
    
    	// Here it must detect the enter key and then go to the main menu
    }
    
    void main()
    {
    	printmenu();
    }
    
    
    void printmenu()
    {
    		int choice;
    	do 
    	{
    		 printf("Menu\n\n");
    		 printf("1. Sum\n");
    		 printf("2. Rest\n");
    		 printf("3. Exit\n");
    		 scanf("%d",&choice);
      
    		 switch (choice)
     		{
    			1:
      		 	sum();
              		break;
         			2: /*Call function here to do the required operation*/
          			break;
         			3: 
    			printf("Goodbye\n"); 
             		break;
        			default: printf("Wrong Choice. Enter again\n");
                        break;
     		}	 
     
    	} while (choice != 3);
    	
    }

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    You didn't use the switch statement correctly, it's meant to be something like this:

    Code:
    switch(choice)
    {
            case 1: printf("You have entered choice 1\n");
                        break;
            case 2: printf("You have entered choice 2\n");
                        break;
            default: printf("Invalid choice!\n");
                        break;
    }
    And also don't use void main() use int main(void) or int main(int argc, char** argv) instead.

    This might give you a better overview, you can add in error control if you need it.

    Code:
    #include <stdio.h>
    
    void load_menu(void);
    void sum(void);
    void rest(void);
    
    int main(int argc, char** argv)
    {
    	load_menu();
    	return 0;
    }
    
    void load_menu(void)
    {
    	int choice;
    
    	do 
    	{
    		printf("Menu\n\n");
    		printf("1. Sum\n");
    		printf("2. Rest\n");
    		printf("3. Exit\n");
    		scanf("%d",&choice);
    
    		switch(choice)
    		{
    			case 1: sum();
    				break;
    			case 2: rest();
    				break;
    			case 3: printf("Quitting program!\n");
    				exit(0);
    				break;
    			default: printf("Invalid choice!\n");
    				break;
    		}
    
    	} while (choice != 3);
    
    }
    
    void sum(void)
    {
    	int num1, num2;
    	int ch;
    	
    	printf("Enter number 1: ");
    	scanf("%d",&num1);
    	printf("Enter number 2: ");
    	scanf("%d",&num2);
    
    	printf("\nThe sum of the numbers was: %d",num1+num2);
    
    	/* Flushes input buffer from the newline from scanf() */
    	while ( (ch = getchar()) != '\n' && ch != EOF) ;
    
    	printf("\n\nPress ENTER to continue.");
    	while ( (ch = getchar()) != '\n' && ch != EOF)
    		;
    
    	return;
    }		
    
    void rest(void)
    {
    	int ch;
    	printf("Sleepy sleepy... zZZzZzZz\n");
    	printf("You now feel awake again!\n");
    
    	/* Flushes input buffer */	
    	while ((ch = getchar()) != '\n' && ch != EOF) ;
    
    	printf("\n\nPress ENTER to continue.");
    	while ((ch = getchar()) != '\n' && ch != EOF)
    		;
    
    	return;
    }
    Last edited by 0rion; 04-09-2005 at 11:08 PM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    Thank you very much. Its working now.

    A last question is How Can I Erase the whole screen and if its an easy way to make a menu centered=??


    I would do it as

    *********************
    * MENU *
    * 1. sUM *
    *********************

    And is it possible to color the characters or the menu in DOS? I want to HIGHLIGHT the * chars and the MENU TITLE

    Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    A last question is How Can I Erase the whole screen
    If you are working on Windows, then you can use clrscr() function in conio.h.

    If on unix platform the use "system("clear")" to clear the screen.

    For all other questions, you are looking at graphics programmming. If on Windows you can look into the BGI graphics library which is supplied with Borland C compiler. If on Unix, "ncurses" is the solution.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course you could always just read the FAQ too...

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

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    I'm sure he is grateful that someone did his homework for him.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I dont understand the last part you gave me.


    How should I use that on my program?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, I don't know. Perhaps the FAQ will tell you. Don't you even understand basic logic? Where do you think you'd put something to potentially clear the screen? Hey, I know, how about some place in your program where you want it to clear the screen!


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

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I alredy included conio.h and I got this
    C:\Archivos de programa\PellesC\Projects\3\main.c(14): warning #2027: Missing prototype for 'clrscr'.
    C:\Archivos de programa\PellesC\Projects\3\main.c(58): warning #2027: Missing prototype for 'exit'.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Looks like you don't have clrscr in your conio.h.
    2) exit is not prototypes in conio.h, it's in stdlib.h.

    This is why you either need to take a moment to look things up on your own (ie: You'd be done by now, instead of waiting for some one to answer you.), and/or have a good reference book around (and use it).

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

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    If I dont have clrscr in my conio.h what should I do?

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    . I am having problems using Borland specific functions such as clrscr()

    Include conio.h to your source, and add C:\Dev-C++\Lib\conio.o to "Further Object Files" in Project Options (where C:\Dev-C++ is where you installed Dev-C++)


    I did this as in the FAQ but there is no conio.o

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Since Pelles C is not Dev-C++, why not try some of the other methods mentioned in the FAQ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  2. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  3. Checking Menu Items
    By Olidivera in forum Windows Programming
    Replies: 5
    Last Post: 06-12-2005, 01:12 PM
  4. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM
  5. Replies: 6
    Last Post: 09-12-2001, 10:35 PM