Like Tree2Likes
  • 2 Post By christop

Return to main menu issue

This is a discussion on Return to main menu issue within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h>#include <stdlib.h> #include <string.h> // function prototype int sale_menu(); int report_menu(); void AddSalesMan(); void QtrReport(); void SalesComm(); // ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Return to main menu issue

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    // function prototype
    int sale_menu(); 
    int report_menu(); 
    void AddSalesMan(); 
    void QtrReport(); 
    void SalesComm();
    
    
    
    
    // sales processing system menu function
    int sale_menu()
    {
    	int menu;
    	system("cls");
    	printf("Sales Processing System\n");
    	printf("-----------------------\n\n");
    	printf("1. Add Salesman Records\n");
    	printf("2. Reports Generation\n");
    	printf("3. Modify Salesman Records\n");
    	printf("4. Delete Salesman Records\n\n");
    	printf("0. Exit\n");
    	printf("Your choice: ");
    	scanf("%d", &menu);
    	return menu;
    	printf("\n");
    }
    // reports generation menu function
    int report_menu()
    {
    	int report;
    	system("cls");
    	printf("Reports Generation\n");
    	printf("------------------\n\n");
    	printf("1. View Quarterly Sales Report\n");
    	printf("2. View Individual Salesman Commission\n\n");
    	printf("0. Return to Main Menu\n");
    	printf("Your choice: ");
    	scanf("%d", &report);
    	return report;
    }
    // function to add salesman
    void AddSalesMan()
    {
    	printf("add salesman\n");
    	system("pause");
    } // end function AddSalesman 
    
    
    // function to generate report
    void QtrReport()
    {
    	printf("Qtr Report\n");
    	system("pause"); // pause the program before it exits	
    } // end function QtrReport
    
    
    void SalesComm()
    {
    	printf("Sales Commission\n");
    	system("pause");
    }
    
    
    // function main begins program execution
    int main()
    {
    	int getMenu = 10, getReport = 10; // menu selector
    	// call and assign sale_menu function to getMenu
    	getMenu = sale_menu();
    	printf("\n");
    	
    	while (getMenu != 0)
    	{
    		// Execute command based on user input
    		switch(getMenu)
    		{
    			// call AddSalesMan function
    			case 1:
    				AddSalesMan();
    				break;
    			// call and assign report_menu to getReport
    			case 2:
    				// Prompt and read report to getReport
    				getReport = report_menu();
    				while (getReport != 0)
    				{
    					// call QtrReport function
    					if (getReport == 1)
    						QtrReport(); 
    					else if (getReport == 2)
    						SalesComm();
    					// Prompt and read report to getReport
    					getReport = report_menu();
    				} // end while
    				break;
    		} // end switch
    	}// end while 
    	system("pause"); // pause the program before it exits	
    	return 0;
    } // end main
    Why I cannot return to main menu from Report Generation?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    sale_menu() is only called before the (outer) while loop. It also needs to be called in the body of that while loop.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Code:
    switch (getReport = report_menu())
                    {
                        while (getReport != 0)
                        {
                            // call QtrReport function
                            case 1:
                                QtrReport();
                                break;
                            case 2:
                                SalesComm();
                                break;
                            getReport = report_menu();
                        } // end while
                    }// end switch
    Thanks. Another question. Is switch cannot support while loop?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by ulti-killer View Post
    Is switch cannot support while loop?
    Why not having the switch inside the loop?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ulti-killer View Post
    Another question. Is switch cannot support while loop?
    No, that's not valid C syntax.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by AndiPersti View Post
    No, that's not valid C syntax.

    Bye, Andreas
    It is valid syntax (see Duff's Device), but it's simply bad style in most instances, including this one.
    stahta01 and AndiPersti like this.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I stand corrected. Sorry for the misinformation.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main menu
    By bazzano in forum C Programming
    Replies: 25
    Last Post: 04-25-2007, 06:18 AM
  2. Returning to main menu
    By Crash1322 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 04:27 PM
  3. Beautifying a main menu screen
    By fkheng in forum C Programming
    Replies: 2
    Last Post: 07-03-2003, 06:29 AM
  4. Going back to a main menu from anywhere
    By Gades in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 03:22 PM

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