Avatar of drisden1
drisden1

asked on 

Tax Calculator in C

Ok folks, this is a homework assignment and I have a good idea of what I am doing but I need someone to check why my exit is not working properly and how I can stop the loop when someone hits a character key and not a number. It calculates correctly but when I want to quit or exit I still get the prompt to input an amount. These loops are kicking my rear again....thanks in advance and here is my code.
/*File Name: Tax Calculator
/*Version 1.04, 27 February 2009. Original version, 23 February 2009./
 
#include <stdio.h>
#include <system.h>
#include <ctype.h>
#include <stdlib.h>
//     Declare the starting point
void main()
{
//     Declare and Initialize a float variable for tax rate, purchase amount, and sales tax
     int iResponse;
     #define fDelMarTaxRate 7.25 
     #define fEncinitasTaxRate 7.5
     #define fLaJollaTaxRate 7.75
     float fDelMarSalesTax;
     float fEncinitasSalesTax;
     float fLaJollaSalesTax;
     float fPurchaseAmount;
           
//     Calculate sales tax for Del Mar, Encinitas, La Jolla in dollars
     
     fDelMarSalesTax = fPurchaseAmount * fDelMarTaxRate / 100;
     fEncinitasSalesTax = fPurchaseAmount * fEncinitasTaxRate / 100;
     fLaJollaSalesTax = fPurchaseAmount * fLaJollaTaxRate / 100;
    
//     Display beginning of program and selections
	printf("\n\t\t\tKudler Fine Foods Tax Calculator\n");
	
	printf("\n\t1. Del Mar\n");
	printf("\t2. Encinitas\n");
	printf("\t3. La Jolla\n");
	printf("\t4. End Program\n");
	
	printf("\n\tSelect 1-3 to see tax rates and press enter or 4 to exit: ");//Lets user know what tp input
	do {    //Begin loop  
 
	scanf("%d", &iResponse);
		if  (iResponse == 1 || iResponse <= 3) //Validates user input 
		{
		printf("\n\tType purchase amount\n\tThen Press ENTER: "); //display for user and prompt for input
		}
		else  	
		{
		printf("\nInvalid entry. Please select another city number\n");//Shows error and prompts user for a correct entry
		}
			
	scanf("%f", &fPurchaseAmount);	
	
     switch (iResponse){ //select case number
   
//     Display sales tax for Del Mar, Encinitas, La Jolla on the purchase amount and the total cost     
       case 1:
	  fDelMarSalesTax = fPurchaseAmount * fDelMarTaxRate / 100;
          printf("\n\tSales Tax Due in Del Mar on a purchase price of $%.2f \t\t\tThe Tax is $%.2f Total Price is $%.2f",
          fPurchaseAmount, fDelMarSalesTax, fPurchaseAmount+fDelMarSalesTax);
          
          printf("\n	Choose another city or 4 to end: ");//prompt user for input
          break;
      
       case 2:
	  fEncinitasSalesTax = fPurchaseAmount * fEncinitasTaxRate / 100;
          printf("\n\tSales Tax Due in Encinitas on a purchase price of $%.2f \t\t\tThe Tax is $%.2f Total Price is $%.2f",
          fPurchaseAmount, fEncinitasSalesTax, fPurchaseAmount+fEncinitasSalesTax);
          
          printf("\n	Choose another city or 4 to end: ");//prompts user for input 
          break;
     
       case 3:
	  fLaJollaSalesTax = fPurchaseAmount * fLaJollaTaxRate / 100;
          printf("\n\tSales Tax Due in La Jolla on a purchase price of $%.2f \t\t\tThe Tax is $%.2f Total Price is $%.2f",
          fPurchaseAmount, fLaJollaSalesTax, fPurchaseAmount+fLaJollaSalesTax);
          
          printf("\n	Choose another city or 4 to end: ");//prompt user for input
          break;
 
       case 4:
          printf("\n\tThank You for using the Kudler Fine Foods Tax Calculator\n"); //ends program
          
	} //end switch
	}  while (iResponse!=4); //end loop    
getch();     
}//end main

Open in new window

C

Avatar of undefined
Last Comment
drisden1

8/22/2022 - Mon