using for loop and functions to create a basic menu

This is a discussion on using for loop and functions to create a basic menu within the C Programming forums, part of the General Programming Boards category; hi, I'm trying to make a function that when it is executed asks the user for three options multilplication, division ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    using for loop and functions to create a basic menu

    hi, I'm trying to make a function that when it is executed asks the user for three options multilplication, division and exit. If the user the user makes a wrong option, the program re-executes asking the user to enter the right option and make the selection again. Can someone please help me on where I'm going wrong.Here is my code:

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int print_Menu();
    
    int main(void)
    {
    printf("This program tests you with ten questions.\n 1) Multiplication\n 2) Division\n 3) Exit\n Please make a selection (1-3)", print_Menu);
    
    
    
    return 0;
    }
    
    
    int print_Menu()
    {
       int i;
       for (i=1; i<4; i++)
    	{
    	    scanf("%d", &i);
    	    if (i==1)
    	    printf("Please give the answers to the following additions:");
    
    	    if (i==2)
    	    printf("Please give the answers to the following division questions:");
    	    if (i==3)
    	    printf("Press the Enter key to exit");
    	    else
    	    printf("Incorrect choice entered: Please select from one to three numbers");
    
    
    	}
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A few problems...

    Your print menu function doesn't actually print a menu...

    The printf() call doesn't display the result of print_menu() and print_menu() doesn't return a value... so you need to make that two separate calls.

    You never ask the user for their choice ... (hint: getchar() or scanf() )

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a basic layout of a menu function that you'd call from main(). The switch statement needs to be filled in with your menu option actions when you make a choice, of course.

    Note especially the do while loop, that keeps repeating the printing of the menu, until the user wants to quit. A for loop just won't do it nearly as clearly.

    Code:
    int menu (void)
    {
      double hi_score, lo_score, avg_score; //change hi and lo to int's.
      int choice, scores[SIZE];
      get_score(scores);
    
      do {
            printf("\nMENU:");
            printf("\nView HIGH SCORE enter    1");
            printf("\nView LOW SCORE enter     2");
            printf("\nView AVERAGE SCORE enter 3");
            printf("\nQUIT enter               4");
            printf("\nEnter your choice: ");
            scanf("%d", &choice);
            (void) getchar(); //clean the input of newline
    
            switch (choice) {
            case 1: //your code might call many other functions here
              break;
            case 2: //to handle all these possible choices
              break;
            case 3: //your code here
              break;
            case 4: //your code here
              break;
            default:
              printf("Error! Try again please\n");
            }
      }while(choice != 4);
    
      return 0; //or (0)
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts on Menu System for Book Inventory
    By curlious in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 04:32 AM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 11:56 AM
  3. Trying to create valid home-made C++ functions.
    By Alphacentric666 in forum C++ Programming
    Replies: 1
    Last Post: 12-25-2002, 09:26 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM

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