Thread: menu driven program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    menu driven program

    ok, task is to create a menu driven program that asks for 5 integers and asks if you want to find sum, average, largest, or smallest. I have created some of the program but i am not lost. how can i get this to run? this is my current code:

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //Function Declarations
    int option;
    int small(int u, int w, int x, int y, int z);
    int large(int u, int w, int x, int y, int z);
    int sum(int u, int w, int x, int y, int z);
    int avg(int u, int w, int x, int y, int z);
    
    
        int one;
        int two;
        int three;
        int four;
        int five;
    
    
    int main(void)
    {
    //Local Declarations
    
    
    
    
    //Statements
        printf  ("\t***********************");
        printf("\n\t*        MENU          ");
        printf("\n\t*                     *");
        printf("\n\t*   1. SMALLEST       *");
        printf("\n\t*   2. LARGEST        *");
        printf("\n\t*   3. SUM            *");
        printf("\n\t*   4. AVERAGE        *");
        printf("\n\t)**********************");
    
    
        printf("\n Please type your choice: ");
        scanf ("%d", &option);
        return option;
    
    
        printf("\n Please enter 5 integers:");
        scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);
    
    
        switch(option)
            {
            case 1: small(one, two, three, four, five);
                    break;
            case 2: large(one, two, three, four, five);
                    break;
            case 3: sum(one, two, three, four, five);
                    break;
            case 4: avg(one, two, three, four, five);
            }
        return 0;
    }   // main
    
    
    int small(int u, int w, int x, int y, int z)
    {
        if (one<two && one<three && one<four && one<five);
            printf("%d", one);
    
    
        else if (two<one && two<three && two<four && two<five);
            printf("%d", two);
    
    
        else if (three<one && three<two && three<four && three<five);
            printf("%d", three);
    
    
        else if (four<one && four<two && four<three && four<five);
            printf("%d", four);
    
    
        else
            printf("%d", five);
    }
    
    
    int large(int u, int w, int x, int y, int z)
    {
        if (one>two && one>three && one>four && one>five);
            printf("%d", one);
    
    
        else if (two>one && two>three && two>four && two>five);
            printf("%d", two);
    
    
        else if (three>one && three>two && three>four && three>five);
            printf("%d", three);
    
    
        else if (four>one && four>two && four>three && four>five);
            printf("%d", four);
    
    
        else
            printf("%d", five);
    }
    
    
    int sum(int u, int w, int x, int y, int z)
    {
        return(u+w+x+y+z);
    }
    
    
    int avg(int u, int w, int x, int y, int z)
    {
        return((u+w+x+y+z)/5);
    }
    and current errors:

    else without previous if for function small & large.

    thanks for any help in advance!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    if (one<two && one<three && one<four && one<five);
    Get rid of all the semicolons at the end of if and else if lines.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > if (one<two && one<three && one<four && one<five);
    Remove the ; at the end of every one of your conditions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Quote Originally Posted by Salem View Post
    > if (one<two && one<three && one<four && one<five);
    Remove the ; at the end of every one of your conditions.
    thanks!! i also had to remove the

    option return;

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ok it runs fine but the sum and average functions dont work for some reason.... it doesnt return anything. any ideas?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It returns something, but you're not printing that result. Try putting printf("sum is %d\n", sum(...)) in your case statement in main.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Look at your functions large() and small(). Currently, whenever you pass it something, it isn't used.

    So get rid of the globals and start passing things for a reason.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    7

    try to simplify your functions.. going to greedy with your logic..

    try taking input in an array so that functions like large can be simplified..translates to lesser chances of a mistake and less typing :|
    Code:
    int input[5];
    
    scanf ("%d %d %d %d %d", input,input + 1,input + 2,input + 3,input + 4);
     
    
    int large(int* input)
    {
    	int i,max = input[0];
    	for(i=0;i<5;i++)
    	{
    		if(input[i] > max)
    		max = input[i];
    	
    	}	    
    printf("%d",&max);
    }

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by getnitin15 View Post
    simplified..translates to lesser chances of a mistake and less typing
    Further:
    Code:
    int input[5], i;
    
    for (i = 0; i < 5; i++) scanf("%d", &input[i]);
    Note that %d will skip intervening whitespace.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    thanks everyone, i got it all working properly!

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    how do i get the program to stop after it prints "INVALID CHOICE"?
    Code:
    /*
    This is a menu driven program that prompts a user for either the smallest, largest, sum, or average for a group of 5 integers
    Written by: Thomas Garrison
    Date: February 27, 2011
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    // Function Declarations
    int option;
    int menu(void);
    int small(int u, int w, int x, int y, int z);
    int large(int u, int w, int x, int y, int z);
    int sum(int u, int w, int x, int y, int z);
    int avg(int u, int w, int x, int y, int z);
    
    
    int a;
    int b;
    
    
    int one;
    int two;
    int three;
    int four;
    int five;
    
    
    int main(void)
    {
    
    
    // Statements
        option= menu();
        printf("\n Please enter 5 integers:");
        scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);
    
    
        switch(option)
            {
            case 1: small(one, two, three, four, five);
                    break;
            case 2: large(one, two, three, four, five);
                    break;
            case 3: sum(one, two, three, four, five);
                    break;
            case 4: avg(one, two, three, four, five);
            }
        return 0;
    }   // main
    
    
    // Functions
     int menu(void)
    {
        // Local Declarations
        int option;
        // Statements
        printf  ("\t***********************");
        printf("\n\t*        MENU          ");
        printf("\n\t*                     *");
        printf("\n\t*   1. SMALLEST       *");
        printf("\n\t*   2. LARGEST        *");
        printf("\n\t*   3. SUM            *");
        printf("\n\t*   4. AVERAGE        *");
        printf("\n\t)**********************");
        printf("\n Please type your choice: ");
        scanf ("%d", &option);
        if (option>4)
        {
            printf("INVALID CHOICE");
        }
        return option;
    }
    
    
    int small(int u, int w, int x, int y, int z)
    {
        // Statements
        if (one<two && one<three && one<four && one<five)
            printf("%d", one);
    
    
        else if (two<one && two<three && two<four && two<five)
            printf("%d", two);
    
    
        else if (three<one && three<two && three<four && three<five)
            printf("%d", three);
    
    
        else if (four<one && four<two && four<three && four<five)
            printf("%d", four);
    
    
        else
            printf("%d", five);
    }
    
    
    int large(int u, int w, int x, int y, int z)
    {
        //Statements
        if (one>two && one>three && one>four && one>five)
            printf("%d", one);
    
    
        else if (two>one && two>three && two>four && two>five)
            printf("%d", two);
    
    
        else if (three>one && three>two && three>four && three>five)
            printf("%d", three);
    
    
        else if (four>one && four>two && four>three && four>five)
            printf("%d", four);
    
    
        else
            printf("%d", five);
    }
    
    
    int sum(int u, int w, int x, int y, int z)
    {
        // Statements
        int a = one + two + three + four + five;
        printf("%d", a);
        return(0);
    }
    
    
    int avg(int u, int w, int x, int y, int z)
    {
        // Statements
        int b = (one + two + three + four + five)/5;
        printf("%d", b);
        return(0);
    }

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    its due in an hour....

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Right after you print "INVALID CHOICE", you need to exit: exit(3): cause normal process termination - Linux man page.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ok but the page you are referring to does not operate.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    nevermind, i got it! i just used exit(0);
    thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu driven program
    By kenadams in forum C Programming
    Replies: 9
    Last Post: 10-27-2011, 01:38 AM
  2. menu driven program help
    By vallamrao in forum C Programming
    Replies: 2
    Last Post: 09-10-2010, 04:56 AM
  3. Menu driven program
    By paushali in forum C Programming
    Replies: 10
    Last Post: 11-26-2007, 10:52 AM
  4. Menu-Driven Program Help...
    By eun-jin in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 02:58 PM