Menu program.

This is a discussion on Menu program. within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a menu program that will be broken down into a series of calls to function ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    6

    Menu program.

    I am trying to write a menu program that will be broken down into a series of calls to function for each of the menu items. Two of the menu items will be simple programs which I wrote.

    I want two of the functions to run one of the two programs I am trying to include as items in the menu.

    So far I am only familiar with variables, loops, if statements, and I just learned how to write functions.

    The problem I am have is that I don't quite understand how to write a function that will run one of the two programs. Also I am having a hard time writing the program in away that would allow the user to select the menu items.

    Could some please help me figure out how to write the program properly.
    Last edited by Andrew Gaupp; 08-19-2013 at 04:29 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I see too many words and not a line of code.. :/

    Try to be exact at your goal and post your attempt.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,208
    Keep practicing functions. One way to approach this is to have a "menu" function that is called from main, perhaps something like this:

    Code:
    int menu(void)
    {
        // print menu items
        // prompt for input
        // read input
        // validate input
        // return result
    }
    Then you would have separate functions associated with each menu item. Have a variable in main read the value returned from the menu function, and use a switch statement to run the appropriate function. For added pleasure, put this menu function and switch statement in a loop.

    Two of the menu items will be simple programs which I wrote.

    The problem I am have is that I don't quite understand how to write a function that will run one of the two programs.
    I would advise against using functions to run other programs you wrote, especially if they are simple. Instead, take those simple programs you wrote and turn them into functions of their own. Here is a simple example to illustrate the point:

    Code:
    // *** Program A - find sum ***
    
    #include <stdio.h>
    
    int main(void)
    {
        int x = 9;
        int y = 5;
        int z = 0;
    
        z = x + y;
    
        printf("Sum is %d\n",z);
    
        return 0;
    }
    
    // *** Program B - find difference***
    
    #include <stdio.h>
    
    int main(void)
    {
        int x = 9;
        int y = 5;
        int z = 0;
    
        z = x - y;
    
        printf("Difference is %d\n",z);
    
        return 0;
    }
    
    // *** Final program - each separate "program" has been refactored into functions ***
    
    #include <stdio.h>
    
    int findSum(int a, int b);
    int findDifference(int a, int b);
    
    int main(void)
    {
        int x = 9;
        int y = 5;
        int z = 0;
    
        z = findSum(x,y);
        printf("Sum is %d\n",z);
    
        z = findDifference(x,y);
        printf("Difference is %d\n",z);
    
        return 0;
    }
    
    int findSum(int a, int b)
    {
        int z = 0;
    
        z = a + b;
        return z;
    }
    
    int findDifference(int a, int b)
    {
        int z = 0;
    
        z = a - b;
        return z;
    }
    Again, please note that this is a contrived example meant only to illustrate my point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATM Menu Program
    By worl4125 in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2013, 11:45 PM
  2. Help with menu program
    By leroyjenkens in forum C Programming
    Replies: 1
    Last Post: 06-15-2012, 03:21 PM
  3. Need big help with a menu program
    By angelofmyst in forum C Programming
    Replies: 7
    Last Post: 12-18-2010, 06:34 PM
  4. Menu program
    By DerrickakaDRoC in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 04:09 PM
  5. A Menu Program
    By Aidanjames86 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2006, 05:44 AM

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