Thread: Menu driven program

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

    Menu driven program

    hi guys im jus wondering wat i can do to get my menu to work. What happens atm is ill enter an option, get the correct output then it will tell me to press any key to continue. At this point it goes back to the menu but jus says "press any key to continue" and then it closes. Code below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define txt "critpathinfo.txt"
    
    
    
    
    
    
    
    
    int main()
    {
        char choice;
        int a, b, c;
    
    
    
    
        printf("              Welcome to Critical Path Analysis              \n");
        printf("_____________________________________________________________\n");
        printf(" a --- print event number, maximum number of days for a task,\n       total number of days for the project completion.\n");
        printf("\n b --- print the event number and task number for all tasks\n       requiring more than 5 days.\n");
        printf("\n c --- print the number of each event, a count of number of tasks\n       within the even.\n");
        printf("\n q --- quit the program\n");
        printf("_____________________________________________________________\n");
        printf("         Please make your choice:  ");
        scanf("%c",&choice);
        if(choice=='a')
        {
            a= func_a();
        }
    
    
        if(choice=='b')
        {
            b= func_b();
        }
        if(choice=='c')
        {
            c=func_c();
        }
    
    
        
    }
    
    
        int func_a()
        {
    
    
            int e, t, d;
            int event=1;
            int numdays=0;
            int totald=0;
    
    
            FILE* data;
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file \n");
            else
            {
    
    
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                {
                    if(event==e)
                    {
                        if(d>numdays)
                            numdays=d;
                    }
                    else
                    {
                        totald=totald + numdays;
                        printf("\n%d         %d",event,numdays);
                        event=e;
                        numdays=d;
                    }
    
    
                }
                     printf("\n%d         %d",event,numdays);
                     totald=totald+numdays;
                     printf("\nTotal days to complete the project: %d\n",totald);
            }
    
    
            fclose(data);
        system("PAUSE");
        return main();
        }
    
    
    
    
        int func_b()
        {
            int e, t, d;
            FILE* data;
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file \n");
            else
            {
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                if(d>5)
                    printf("\n%d         %d",e,t);
    
    
            }
        fclose(data);
        system("PAUSE");
        return main();
        }
    
    
        int func_c()
        {
            int e, t, d;
            int event=1;
            int numtasks=0;
            FILE* data;
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file \n");
            else
            {
    
    
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                {
    
    
                    if(event==e)
                    {   ++numtasks;
                        event=e;
                    }
                    else
                    {   printf("\n%d         %d",event,numtasks);
                        numtasks=1;
                        event=e;
    
    
    
    
                    }
    
    
                }
            printf("\n%d         %d",event,numtasks);
            }
    
    
        fclose(data);
        system("PAUSE");
        return main();
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your menu isn't in a loop, so why would it repeat?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why are you calling main recursively?
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Quote Originally Posted by TheBigH View Post
    Why are you calling main recursively?
    :/ i guess i dnt rly understand functions all that well. I figured since i want it to go back to the menu i should return main()

    and okay i added a do while loop for the menu. im not sure why but if i put in 'a' and then the second time around put in 'q' it jus gets stuck on press any key to continue and wont exit

    Code:
     do    {
    
    
    scanf("%c",&choice);
    
            if(choice=='a')
            {
                a= func_a();
            }
    
    
            if(choice=='b')
            {
                b= func_b();
            }
            if(choice=='c')
            {
                c=func_c();
            }
        }
        while(choice!='q');
     system("PAUSE");
        return 0;
    Last edited by kenadams; 10-26-2011 at 10:44 PM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    anyone down to help?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    ok i fixed it up a bit and its workin well. the only thing i ask of one of u is to tell me how to bring the menu up(the printf stuff) everytime after it 'a' 'b' or 'c'


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define txt "critpathinfo.txt"
    
    
    
    
    
    
    
    
    int main()
    {
        char choice;
        int a, b, c;
        
    
    
        printf("              Welcome to Critical Path Analysis              \n");
        printf("_____________________________________________________________\n");
        printf(" a --- print event number, maximum number of days for a task,\n       total number of days for the project completion.\n");
        printf("\n b --- print the event number and task number for all tasks\n       requiring more than 5 days.\n");
        printf("\n c --- print the number of each event, a count of number of tasks\n       within the even.\n");
        printf("\n q --- quit the program\n");
        printf("_____________________________________________________________\n");
        printf("         Please make your choice:  ");
    
    
    
    
        do
        {
    
    
            scanf("%c",&choice);
    
     switch(choice)
            {
                case 'a':
                a= func_a();
                break;
    
    
                case 'b':
                b=func_b();
                break;
    
    
                case 'c':
                c=func_c();
                break;
            }
        }
        while(choice!='q');
    
    
        system("PAUSE");
        return 0;
    
    
    
    
    
    
    }
    
    
        int func_a()
        {
    
    
            int e, t, d;
            int event=1;
            int numdays=0;
            int totald=0;
    
    
            printf("\nEvent number       Max days\n");
            printf("===============================");
            FILE* data;
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file \n");
            else
            {
    
    
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                {
                    if(event==e)
                    {
                        if(d>numdays)
                            numdays=d;
                    }
                    else
                    {
                        totald=totald + numdays;
                        printf("\n   %d                %d",event,numdays);
                        event=e;
                        numdays=d;
                    }
    
    
                }
                     printf("\n   %d                %d",event,numdays);
                     totald=totald+numdays;
                     printf("\n===============================\n\n");
                     printf("\nTotal days to complete the project: %d\n",totald);
            }
    
    
            fclose(data);
    
    
        return 0;
        }
    
    
    
    
        int func_b()
        {
            int e, t, d;
            FILE* data;
            printf("\nEvent number     Task number\n");
            printf("===============================");
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file");
            else
            {
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                if(d>5)
                    printf("\n   %d             %d",e,t);
    
    
            }
            printf("\n===============================\n");
        fclose(data);
    
    
        return 0;
        }
    
    
        int func_c()
        {
            int e, t, d;
            int event=1;
            int numtasks=0;
            FILE* data;
            printf("\nEvent number     Number of Tasks");
            printf("\n===============================");
    
    
            data= fopen(txt,"r");
            if(data==NULL)
                printf("Error opening input file \n");
            else
            {
    
    
                while(fscanf(data,"%d%d%d",&e,&t,&d)==3)
                {
    
    
                    if(event==e)
                    {   ++numtasks;
                        event=e;
                    }
                    else
                    {   printf("\n   %d               %d",event,numtasks);
                        numtasks=1;
                        event=e;
    
    
    
    
                    }
    
    
                }
            printf("\n   %d               %d",event,numtasks);
            }
            printf("\n===============================\n");
        fclose(data);
    
    
        return 0;
        }
    Last edited by kenadams; 10-26-2011 at 11:45 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Seems pretty obvious. If you need it to happen every time the loop happens, put it in the loop:
    Code:
    do
        show menu text
        ask for menu option
        read menu option
        do menu thing based on option
    while menu option != quit
    You just need to stop and think about what it is you are trying to do. Put it into word, then read it back, sometimes that's all it takes for it to click.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    okay i made the printing of the menu into a function. im tryna input it into my switch statement but it wont work and i have no clue why.

    the menu() jus prints the menu
    Code:
    case 'a':
                a= func_a();
                printf("menu?");
                scanf("%c",&repeat_menu);
                if(repeat_menu=='y')
                    printmenu=menu();
                break;

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    can anyone tell me why thats not working?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Take lines 19 to 26, and paste them into a new function called menu.
    Code:
    void menu ( void ) {
      // here
    }
    And call menu(); when you need to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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