Thread: help with a switch and a loop

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Unhappy help with a switch and a loop

    I am writing this program fro a class and it will compile in bloodshed, but will not give me the total price that I am trying to get to. The steps are to pick a location, enter a purchase amount and it is suppose to give you the tax and total amounts, but it stops after the purchase amount is entered. Can anyone see my error?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    
    {
    //declare sales tax rates and initialize variables    
    float purchase = 0.0;
    float delmar_rate = 0.0725;
    float encinitas_rate = 0.075;
    float lajolla_rate = 0.0775;
    float totaltax1= 0;
    float totaltax2= 0;
    float totaltax3 = 0;
    float totalpurchase1 = 0;
    float totalpurchase2 = 0;
    float totalpurchase3 = 0;
    int storelocation = 0;
    
    //enter input items
    printf("Kudler Fine Foods Sales Tax\n");
    printf("\n Please Select:\n 1 - Del Mar\n 2 - Encinitas\n 3 - La Jolla\n");
    printf("\nPlease select a store (1-3): ");
    scanf("%d", &storelocation);
    printf("\nEnter Purchase Total: $");
    	do
    	{
    		scanf("%f",&purchase);
    	
    		//Display error message to user.
    		if (purchase <= 0)
    		{
    			printf("Error: purchase must be greater than 0\n");
    			printf("\nEnter Purchase Total: $");
    		} //if PurchaseAmount <=0)
    		
    	} while (purchase <= 0);
    			
    	//Perform the Calculations.
    totaltax1 = purchase * delmar_rate;
    totaltax2 = purchase * encinitas_rate;
    totaltax3 = purchase * lajolla_rate;
    totalpurchase1 = totaltax1 + purchase;
    totalpurchase2 = totaltax2 + purchase;
    totalpurchase3 = totaltax3 + purchase;
    	
    //print tax amount and total sale
    switch(storelocation){
    case '1':  printf("\nDel Mar Sales Tax:  $%.2d\n", totaltax1);
    printf("Del Mar Total Purchase:  $%.2d\n\n", totalpurchase1);
     
    case '2':  printf("Encinitas Sales Tax:  $%.2d\n", totaltax2);  
    printf("Encinitas Total Purchase:  $%.2d\n\n", totalpurchase2);
     
    case '3':  printf("La Jolla Sales Tax:  $%.2d\n", totaltax3);
    printf("La Jolla Total Purchase:  $%.2d\n\n", totalpurchase3);
    }//end of switch
    
    
    //used to keep console window open
    system("Pause");
      
    return 0;     
    
    }//end of main function

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try an experiment: type 49 for the location and see what happens.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    I tried the experiment

    I typed 49 for the location and 45 for purchase and it gave me big negative numbers.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Remove the single quotes in the switch case.
    I have modified your program into the following.
    Try with that.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    
    {
    //declare sales tax rates and initialize variables
    float purchase = 0.0;
    float delmar_rate = 0.0725;
    float encinitas_rate = 0.075;
    float lajolla_rate = 0.0775;
    float totaltax1= 0;
    float totaltax2= 0;
    float totaltax3 = 0;
    float totalpurchase1 = 0;
    float totalpurchase2 = 0;
    float totalpurchase3 = 0;
    int storelocation = 0;
    
    //enter input items
    printf("Kudler Fine Foods Sales Tax\n");
    printf("\n Please Select:\n 1 - Del Mar\n 2 - Encinitas\n 3 - La Jolla\n");
    printf("\nPlease select a store (1-3): ");
    scanf("%d", &storelocation);
    printf("\nEnter Purchase Total: $");
            do
            {
                    scanf("%f",&purchase);
    
                    //Display error message to user.
                    if (purchase <= 0)
                    {
                            printf("Error: purchase must be greater than 0\n");
                            printf("\nEnter Purchase Total: $");
                    } //if PurchaseAmount <=0)
    
            } while (purchase <= 0);
    
            //Perform the Calculations.
    totaltax1 = purchase * delmar_rate;
    totaltax2 = purchase * encinitas_rate;
    totaltax3 = purchase * lajolla_rate;
    totalpurchase1 = totaltax1 + purchase;
    totalpurchase2 = totaltax2 + purchase;
    totalpurchase3 = totaltax3 + purchase;
    
    //print tax amount and total sale
    switch(storelocation){
    case 1:  printf("\nDel Mar Sales Tax:  $%.2d\n", totaltax1);
    printf("Del Mar Total Purchase:  $%.2d\n\n", totalpurchase1);
    break;
    
    case 2:  printf("Encinitas Sales Tax:  $%.2d\n", totaltax2);
    printf("Encinitas Total Purchase:  $%.2d\n\n", totalpurchase2);
    break;
    
    case 3:  printf("La Jolla Sales Tax:  $%.2d\n", totaltax3);
    printf("La Jolla Total Purchase:  $%.2d\n\n", totalpurchase3);
    break;
    
    default:
         printf("Invalid option\n");
    }//end of switch
    
    
    //used to keep console window open
    system("Pause");
    
    return 0;
    
    }//end of main function
    storelocation is a integer not a character.
    So 1 and '1' is not equal.
    For more details refer the following program.
    Code:
    #include<stdio.h>
    main()
    {
            if(1=='1')
                    printf("true\n");
            else
                    printf("False\n");
    }
    .
    Last edited by kiruthika; 03-22-2010 at 10:31 PM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    The problem is the that you have checked the storelocation values as a character .But it is a integer .

    Code:
      #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    
    {
    //declare sales tax rates and initialize variables
    float purchase = 0.0;
    float delmar_rate = 0.0725;
    float encinitas_rate = 0.075;
    float lajolla_rate = 0.0775;
    float totaltax1= 0;
    float totaltax2= 0;
    float totaltax3 = 0;
    float totalpurchase1 = 0;
    float totalpurchase2 = 0;
    float totalpurchase3 = 0;
    int storelocation = 0;
    
    //enter input items
    printf("Kudler Fine Foods Sales Tax\n");
    printf("\n Please Select:\n 1 - Del Mar\n 2 - Encinitas\n 3 - La Jolla\n");
    printf("\nPlease select a store (1-3): ");
    scanf("%d", &storelocation);
    printf("\nEnter Purchase Total: $");
            do
            {
                    scanf("%f",&purchase);
    
                    //Display error message to user.
                    if (purchase <= 0)
                    {
                            printf("Error: purchase must be greater than 0\n");
                            printf("\nEnter Purchase Total: $");
                    } //if PurchaseAmount <=0)
    
            } while (purchase <= 0);
    
            printf("It is %f \n",purchase);
            //Perform the Calculations.
    totaltax1 = purchase * delmar_rate;
    totaltax2 = purchase * encinitas_rate;
    totaltax3 = purchase * lajolla_rate;
    totalpurchase1 = totaltax1 + purchase;
    totalpurchase2 = totaltax2 + purchase;
    totalpurchase3 = totaltax3 + purchase;
    
    //print tax amount and total sale
    switch(storelocation){
    case 1:  printf("\nDel Mar Sales Tax:  $%.2d\n", totaltax1);
    printf("Del Mar Total Purchase:  $%.2d\n\n", totalpurchase1);
    break;
    case 2:  printf("Encinitas Sales Tax:  $%.2d\n", totaltax2);
    printf("Encinitas Total Purchase:  $%.2d\n\n", totalpurchase2);
    break;
    case 3:  printf("La Jolla Sales Tax:  $%.2d\n", totaltax3);
    printf("La Jolla Total Purchase:  $%.2d\n\n", totalpurchase3);
    default :
    printf("Invalid\n");
    break;
    }//end of switch
    
    
    //used to keep console window open
    //system("Pause");
    
    return 0;
    
    }//end of main function
    Use break and default in the switch statements.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    The first code runs and gives me $0 for sales tax and total.

    The second runs but closes after the purchase amount is put in.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Infact I commented the statement

    Code:
      
    system("Pause");
    Just remove the comment in that statement .

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    remove that statement completely? I have always had to put that in to keep the window open in bloodshed.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    You use the %.2f format specifier in printf inside the switch cases instead of %.2d.
    Your code,
    Code:
    printf("\nDel Mar Sales Tax:  $%.2d\n", totaltax1);
    Change it like the following.
    Code:
    printf("\nDel Mar Sales Tax:  $%.2f\n", totaltax1);

  10. #10
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    When you print the results you used %d .But the values are floating point values . So use %f.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    
    {
    //declare sales tax rates and initialize variables
    float purchase = 0.0;
    float delmar_rate = 0.0725;
    float encinitas_rate = 0.075;
    float lajolla_rate = 0.0775;
    float totaltax1= 0;
    float totaltax2= 0;
    float totaltax3 = 0;
    float totalpurchase1 = 0;
    float totalpurchase2 = 0;
    float totalpurchase3 = 0;
    int storelocation = 0;
    
    //enter input items
    printf("Kudler Fine Foods Sales Tax\n");
    printf("\n Please Select:\n 1 - Del Mar\n 2 - Encinitas\n 3 - La Jolla\n");
    printf("\nPlease select a store (1-3): ");
    scanf("%d", &storelocation);
    printf("\nEnter Purchase Total: $");
            do
            {
                    scanf("%f",&purchase);
    
                    //Display error message to user.
                    if (purchase <= 0.0)
                    {
                            printf("Error: purchase must be greater than 0\n");
                            printf("\nEnter Purchase Total: $");
                    } //if PurchaseAmount <=0)
    
            } while (purchase <= 0.0);
    
            //Perform the Calculations.
    totaltax1 = purchase * delmar_rate;
    totaltax2 = purchase * encinitas_rate;
    totaltax3 = purchase * lajolla_rate;
    totalpurchase1 = totaltax1 + purchase;
    totalpurchase2 = totaltax2 + purchase;
    totalpurchase3 = totaltax3 + purchase;
    
    //print tax amount and total sale
    switch(storelocation){
    case 1:  printf("\nDel Mar Sales Tax:  $%.2f\n", totaltax1);
    printf("Del Mar Total Purchase:  $%.2f\n\n", totalpurchase1);
    break;
    case 2:  printf("Encinitas Sales Tax:  $%.2f\n", totaltax2);
    printf("Encinitas Total Purchase:  $%.2f\n\n", totalpurchase2);
    break;
    case 3:  printf("La Jolla Sales Tax:  $%.2f\n", totaltax3);
    printf("La Jolla Total Purchase:  $%.2f\n\n", totalpurchase3);
    default :
    printf("Invalid\n");
    break;
    }//end of switch
    
    
    //used to keep console window open
    system("Pause");
    
    return 0;
    
    }//end of main function

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    That gives me an invalid option error after I put in the purchase amount.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    What input you have given for the following,
    Please select a store (1-3):
    Here if you give the value which is not between 1-3 then it will give the error "Invalid option".

  13. #13
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    What is the input you gave for the option . Otherwise post the output you got.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Thanks to all who have tried to help with this. I am calling it a night and will check in the morning. Thanks again! I would love to get this figured out. Not just for the grade, but for my own learning.

Popular pages Recent additions subscribe to a feed