Programming menu options

This is a discussion on Programming menu options within the C Programming forums, part of the General Programming Boards category; Code: int main(){ int ans; //Get user's input printmenu(); scanf("%d", &ans); while (ans !=6) { if (ans==1) tripfueleff(); else if ...

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Programming menu options

    Code:
    int main(){
    
    		int ans;
    		
    		//Get user's input
    		printmenu();
    		scanf("%d", &ans);
    
    		while (ans !=6) {
    			
    			if (ans==1) 
    				tripfueleff();
    			else if (ans==2) 
    				getitem();
    			else if (ans==3)
    				printlogo();
    			else if (ans==4)
    				multgame();
    			else if (ans==5)
    				printfuelgraph();
    			
    		}
    }
    		
    //Prints out menu
    void printmenu() {
    	printf("Which of the following options would you like?\n");
    	printf("1) Calculate fuel efficiency for a trip.\n");
    	printf("2) Determine whether or not to go back home and pick up an item.\n");
    	printf("3) Print the car logo to the display.\n");
    	printf("4) Play the multiplication game.\n");
    	printf("5) Print a visual display of the recent fuel efficiency.\n");
    	printf("6) Quit\n");
    }
    That's what I got for a menu so far. But after I choose an option, and the specified function runs, it repeats the function. After the function is done running I need it to go back to the menu instead of repeating. Do I need to add something to the while statement to do this or something to the functions themselves?

    Here's an example of one of the functions:
    Code:
    void getitem() {
    	int mins_dist, mins_before, mins_forgot, total, spare1, spare2;
    	
    	//User input
    	
    	printf("How long does it take you to drive to work (in minutes)?\n");
    	scanf("%d", &mins_dist);
    	
    	printf("How many minutes before work did you start?\n");
    	scanf("%d", &mins_before);
    	
    	printf("How many minutes did it take you to realize you forgot an item?\n");
    	scanf("%d", &mins_forgot);
    	
    	//Determine if you should turn around
    	
    	total = (mins_forgot * 2) + mins_dist;
    	spare1 = mins_before - total;
    	spare2 = mins_before - mins_dist;
    	
    	if (total <= mins_before)
    		printf("Go back home and pick up the item.\nYou will arrive at work with %d minutes to spare.\n", spare1);
    	else
    		printf("Just go to work, you'll have to do without the item.\nYou will arrive at work with %d minutes to spare.\n", spare2);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should move your printmenu() and scanf() calls inside your while loop.
    You may also find that do...while is a better loop structure in this case.

  3. #3
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    Perfect! That was exactly what I need thank you. And i'll definitely look into a do while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Lost BIOS menu options?
    By PJYelton in forum Tech Board
    Replies: 3
    Last Post: 11-14-2004, 08:23 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM
  5. Disabling the main frame menu options
    By MPC_Engr in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2003, 09:04 AM

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