Thread: Can't get loop to keep running

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Can't get loop to keep running

    I'm not sure why, but after I give the input when it prompts me, my program ends.

    Here is my program:

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    
    int main ()
    {
    
    int choice;
    
    double app=0;
    double tom=0;
    double let=0;
    double cuc=0;
    
    
    
    double appcost=0;
    double tomcost=0;
    double letcost=0;
    double cuccost=0;
    
    int counter;
    
    char cancel[20];
    
    double total=0;
    
       do
       {
       printf("MAIN MENU\n"// The main menu
       "============\n"
       "Option   Action\n"
       "------   ------------------------------------------\n"
       "   A   -  Purchase apples\n"
       "   C   -  Purchase cucumbers\n"
       "   L   -  Purchase lettuce\n"
       "   T   -  Purchase tomatoes\n"
       "   !   -  Finalize bill of sale\n"
       "   *   -  Cancel this bill and reset 'for' new customer\n"
       "   &   -  Quit program\n\n"
       "Enter the desired option:"
       );
       
       do
       {
       choice = getchar();
       }
       while (isspace(choice) && choice !=EOF);
       
       choice = toupper(choice);
       
          switch (choice) 
    		{
    		total = app+cuc+let+tom;
    			case 'A': //APPLES
    			printf("Weight in kg of apples:");
    			scanf("%lf\n", &app);
    			appcost= app*3.59;
    			app=app+app;
    			break;
    			
    			case 'C': //CUCUMBERS
    			printf("Number of cucumbers:");
    			scanf("%lf\n", &cuc);
    			cuccost= cuc*2.29;
    			cuc=cuc+cuc;
    			break;
    			
    			case 'L': //LETTUCE
    			printf("Number of heads of lettuce:");
    			scanf("%lf\n", &let);
    			letcost= let*1.49;
    			let=let+let;
    			break;
    			
    			case 'T': //TOMATOES
    			printf("Weight in kg of tomatoes:");
    			scanf("%lf\n", &tom);
    			tomcost= tom*1.29;
    			tom=tom+tom;
    			break;
    			
    			case '!': //BILL OF SALE
    			if (total==0)
    			{
    			printf("No bill of sale. Nothing has been purchased.\n");
    			}
    			else
    			{
    			printf("BILL OF SALE\n"
    			"=============\n");
    			if (appcost!=0)
    			printf("%lf kg apples: $ %lf (TX)\n", app, appcost);	
    			if (cuccost!=0)
    			printf("%lf cucombers: $ %lf (TX)\n", cuc, cuccost);	
    			if (letcost!=0)
    			printf("%lf lettuce: $ %lf (TX)\n", let, letcost);	
    			if (tomcost!=0)
    			printf("%lf tomato: $ %lf (TX)\n", tom, tomcost);
    			double sub=appcost+cuccost+letcost+tomcost;
    			double hst=sub*0.13;
    			double final=sub*1.13;
    			printf("\n");
    			printf("=============================\n");
    			printf("               Subtotal: $  %.2lf  \n", sub);
    			printf("                  HST: $  %.2lf   \n", hst);
    			printf("                 TOTAL: $  %.2lf   \n\n", final);
    			printf("---------------------------------------------------------------\n");
    			
    			
    			}
    			break;
    			
    			case '*': //CANCEL
    			printf("Are you sure you want to cancel the current bill (Yes/No)?");
    			scanf("%[^\n]", cancel);
    		
    			   for(counter=0; counter<strlen(cancel); counter++)
    			   {
    			   cancel[counter] = tolower(cancel[counter]);
    			   	 
    			/*		if(cancel=='yes')
    					 {
    					 
    					 }
    					 if(cancel=='no')
    					 {
    					 
    					 }   */
    			   }
    			   
    				
    			}
    			if (choice=='&');
    			break;
       }
       while (1);
       //while (choice!='&' && choice!=EOF);
        
    	
    
    return 0;
    }

    Thanks for the help in advance!!

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    This is what I am getting when I run it:


    trademark@bravo:~/60-106$ ./a.out
    MAIN MENU
    ============
    Option Action
    ------ ------------------------------------------
    A - Purchase apples
    C - Purchase cucumbers
    L - Purchase lettuce
    T - Purchase tomatoes
    ! - Finalize bill of sale
    * - Cancel this bill and reset 'for' new customer
    & - Quit program

    Enter the desired option:a
    Weight in kg of apples:4
    a
    trademark@bravo:~/60-106$

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
            if (choice=='&');
            break;
    That ; at the end of the if statement means that there is not body to the if statement, so the break happens no matter what choice is, and your program exits. Also, you may want to remove the \n from your scanf calls, it makes it very difficult to enter weights.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running through a loop using pointers
    By boy in forum C Programming
    Replies: 43
    Last Post: 08-19-2011, 04:43 PM
  2. For loop not running properly
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 04-04-2008, 05:27 PM
  3. Running a program in a loop
    By Robert_Sitter in forum C# Programming
    Replies: 7
    Last Post: 01-27-2006, 10:09 AM
  4. functions stops loop from running
    By a1pro in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 02:02 PM
  5. Running triangles through loop
    By shanedudddy2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:59 AM