Thread: Help with sentinel value

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Help with sentinel value

    I basically have the majority of the program written out. But, for some reason the while statement will not end when the sentinel value (variable sentinelMeal) has been reached. Could somebody help me out with this? Do I need to take the 'meals=meals+quantity' out of the switch statement?

    I am not getting any errors or warnings, so I think mostly everything is fine.

    Thanks, any help is appreciated.

    Code:
    #include <stdio.h>
    
    int main ()
    
    	
    
    {	
    	
    	double meals=0;
    	double total=0;
    	int sentinelMeal;
    	int combo;
    	double quantity;
    	double subtotal;
    
    	printf("Please enter the number of meals you would like to order:");
    	scanf("%d",&sentinelMeal);
    
    	
    	while(meals<=sentinelMeal) {
    		
    		printf("Menu\n1) Hamburger, Fries, and drink...................$2.79\n2) Cheeseburger, Fries, and drink................$3.09\n3) Quarterpounder, Fries and drink...............$3.29\n4) Double Cheesburger, Fries, and drink..........$3.89\n5) Grilled Chicken Sandwich, Fries, and drink....$3.19\n6) Fish Sandwich, Fries, and drink...............$3.29\n");
    
    		printf("Enter meal combo number:\n");
    		scanf("%d",&combo);
    
    		printf("Please enter the quantity of this combo you would like to order");
    		scanf("%d",&quantity);
    
    		
    		switch(combo) {
    			case'1':
    				subtotal=2.79*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case'2':
    				subtotal=3.09*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    			
    			case'3':
    				subtotal=3.29*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case'4':
    				subtotal=3.89*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case'5':
    				subtotal=3.19*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case'6':
    				subtotal=3.29*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    		}
    
    	}
    
    	printf("Purchase Receipt %d,total");
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    try using your compiler's debugger, single step through the program and find out what its doing. Or put some print statements in so you can see the value of the variables.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    I think there maybe something wrong with the switch statement, I added a default case, and that comes up no matter what combo number I enter.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    I added a printf statement after the switch statement.
    Code:
     printf("the combo number you entered was %d, combo");
    But when I enter 3, the printf statement comes up with the value 1916, and idea why that would be happening?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Your syntax is wrong for that printf() statement. This works for me:
    Code:
    int combo;
    printf("Please enter the number of meals you would like to order:");
    scanf("%d",&combo);
    printf("Here it is: %d\n", combo);
    By the way, in C++ you use cin>> to input data from the keyboard, and cout<< to display data to the console window.
    Last edited by 7stud; 02-15-2006 at 05:36 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by 7stud
    Your syntax is wrong for that printf() statement. This works for me:
    Code:
    int combo;
    printf("Please enter the number of meals you would like to order:");
    scanf("%d",&combo);
    printf("Here it is: %d\n", combo);
    By the way, in C++ you use cin>> to input data from the keyboard, and cout<< to display data to the console window.
    Yeah, I just figured that out. That was stupid of me.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by 7stud
    Your syntax is wrong for that printf() statement. This works for me:
    Code:
    int combo;
    printf("Please enter the number of meals you would like to order:");
    scanf("%d",&combo);
    printf("Here it is: %d\n", combo);
    By the way, in C++ you use cin>> to input data from the keyboard, and cout<< to display data to the console window.
    Yeah, I think we are getting to that later, I am just beginning with this, so I think this is how they teach us at first.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Okay, this is what I have now. But I am still having the problem with it not ending at the sentinelMeal, I have to go one over it. I am getting a correct total and everything.

    Code:
    #include <stdio.h>
    
    int main ()
    
    	
    
    {	
    	
    	int meals=0;
    	double total=0;
    	int sentinelMeal;
    	int combo;
    	int quantity;
    	double subtotal;
    
    	printf("Please enter the number of meals you would like to order:");
    	scanf("%d",&sentinelMeal);
    
    	
    	while(meals<=sentinelMeal) {
    		
    		printf("Menu\n1) Hamburger, Fries, and drink...................$2.79\n2) Cheeseburger, Fries, and drink................$3.09\n3) Quarterpounder, Fries and drink...............$3.29\n4) Double Cheesburger, Fries, and drink..........$3.89\n5) Grilled Chicken Sandwich, Fries, and drink....$3.19\n6) Fish Sandwich, Fries, and drink...............$3.29\n");
    
    		printf("Enter meal combo number:\n");
    		scanf("%d",&combo);
    
    		printf("Please enter the quantity of this combo you would like to order");
    		scanf("%d",&quantity);
    
    		
    		switch(combo) {
    			case 1:
    				subtotal=2.79*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case 2:
    				subtotal=3.09*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    			
    			case 3:
    				subtotal=3.29*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case 4:
    				subtotal=3.89*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case 5:
    				subtotal=3.19*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			case 6:
    				subtotal=3.29*quantity;
    				total=total+subtotal;
    				meals=meals+quantity;
    				break;
    
    			default:
    				printf("This combo does not exist, Please choose a combo number between 1 and 6.");
    				break;
    		}
    
    		printf("combo you entered was %d", combo);
    	}
    
    	printf("Purchase Receipt %21.2f",total);
    
    	return 0;
    
    }

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Okay, I got it all to work out. Thanks for trying to help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  2. Problem with sentinel values
    By Golfduffer in forum C Programming
    Replies: 3
    Last Post: 08-30-2007, 01:19 AM
  3. SENTINEL question
    By codeHer1 in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:34 AM
  4. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM