Thread: Menu program

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    1

    Menu program

    You need to write a menu driven program (Menu needs to display again and again till user chooses to select option 4 to exit).
    The program allows a user to enter five numbers and then asks the user to select a choice from a menu. The menu should offer following options –

    1. Display the smallest number entered
    2. Display the largest number entered
    3. Display the sum of the five numbers entered
    4. Display the average of the five numbers entered.
    5. Exit
    You must use a "switch" statement in your code to determine what action to take. This menu needs to be repeated again and again till the user chooses option 5 to quit. Provide an error message if an invalid choice is entered.
    Run the program six times, once with each option from 1 to 4 then an invalid option and at the end with option 5 to quit. Each run is to use the following set of data (Note that your program needs to work with any five numbers entered by the user):
    18, 21, 17, 44, 9.
    Submit a word file with IPO, source code and the screenshots of the results of the five program runs.
    insert
    Code:
    int main () {
        int userNumber = 0,
        sum = 0,
        count = 0,
        highNum = 0,
        lowNum = 0,
        lastNum = 0;
        double average;
        
        printf("Enter a number greater than 0: (Enter -4 to stop)\n");
        scanf("%i", &userNumber);
        highNum = userNumber;
        lowNum = userNumber;
        
        while(userNumber != -4) {
            if(userNumber > lowNum && userNumber > highNum)
                highNum = userNumber;
            count = count + 1;
            sum = sum + userNumber;
            
            printf("Enter a number: (-4 to stop)\n");
            scanf("%i", &userNumber);
        }
        average = (double)sum / count;
        
        printf("The sum is: %i\n", sum);
        printf("%i numbers were input\n", count);
        printf("The average is %.2lf\n", average);
        printf("The highest number entered was: %i\n", highNum);
        printf("The last number entered was: %i\n", lastNum);
        printf("The lowest number entered was: %i\n", lowNum);
        return 0;
    }
    This is what I have so far. Can anyone help me ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know how to define your own functions? Personally, what I would do is to define a function for reading in the five numbers, then one function each corresponding to the first 4 menu options. You can then wrap the switch with its function calls in a loop that terminates on a flag that is set when the fifth menu option is selected.

    You already more or less have the implementations of these five functions; it's a matter of separating them into these functions so you can call them precisely when you want.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu Program
    By Matia James in forum C Programming
    Replies: 1
    Last Post: 03-19-2017, 01:17 AM
  2. Program menu
    By Zaid Mashni in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2016, 09:52 AM
  3. Menu program help
    By Andrew Gaupp in forum C++ Programming
    Replies: 1
    Last Post: 01-03-2014, 09:33 PM
  4. Help with menu program
    By leroyjenkens in forum C Programming
    Replies: 1
    Last Post: 06-15-2012, 03:21 PM
  5. A Menu Program
    By Aidanjames86 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2006, 06:44 AM

Tags for this Thread