Thread: using for loop and functions to create a basic menu

  1. #1
    Registered User
    Join Date
    Feb 2011

    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:

    #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");
    	    printf("Incorrect choice entered: Please select from one to three numbers");

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    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
    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.

    int menu (void)
      double hi_score, lo_score, avg_score; //change hi and lo to int's.
      int choice, scores[SIZE];
      do {
            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
            case 2: //to handle all these possible choices
            case 3: //your code here
            case 4: //your code here
              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, 03:32 AM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10: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, 06:53 PM