Hi everybody,
I am in an introduction to C class and here is a program we had to do. Now, what I am trying to do is expand on it a little more and do some error checking. So, if the user inputs something other than the correct amount or the correct store I want it to loop back up and ask again. Can someone help, I have tried using a for loop but I haven't been able to get it to work correctly.
Code:
#include <stdio.h>
// function prototypes
float tax_delmar(float);
float tax_encinitas(float);
float tax_lajolla(float);
float add_total(float, float);
// global variables that can be used by any function
float sales_tax;
float total;
main()
{
//defining and initializing data type
float purchase_amt = 0.0;
char store = '0';
printf("\nWhat is your purchase amount? $");
//receiving user input for the purchase amount
scanf("%f", &purchase_amt);
printf("\nWhat store?\n");
printf("\nPress D for DelMar, E for Encinitas, or L for LaJolla: ");
scanf("%c", &store);
if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'd')
{
//sales tax for the Del Mar store rate
printf("\n\nSales tax for $%.2f at the DelMar store is: $%.2f\n", purchase_amt, tax_delmar(purchase_amt));
//total price with sales tax included for the Del Mar store
printf("\nThe total purchase price at the DelMar store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
}
else if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'e')
{
//sales tax for the Encinitas store rate
printf("\nSales tax for $%.2f at the Encinitas store is: $%.2f\n", purchase_amt, tax_encinitas(purchase_amt));
//total price with sales tax included for the Encinitas store
printf("\nThe total purchase price at the Encinitas store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
}
else if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'l')
{
//sales tax for the La Jolla store rate
printf("\nSales tax for $%.2f at the LaJolla store is: $%.2f\n", purchase_amt, tax_lajolla(purchase_amt));
//total price with sales tax included for the La Jolla store
printf("\nThe total purchase price at the LaJolla store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
}
printf("\nPress return to end program!\n");
//pausing program until user ends program
getch();
return;
}
//calculate the sales tax on the purchase amount for the Del Mar store
float tax_delmar(float purchase_amt)
{
sales_tax = purchase_amt * 0.0725;
return sales_tax;
}
//calculate the sales tax on the purchase amount for the Encinitas store
float tax_encinitas(float purchase_amt)
{
sales_tax = purchase_amt * 0.075;
return sales_tax;
}
//calculate the sales tax on the purchase amount for the La Jolla store
float tax_lajolla(float purchase_amt)
{
sales_tax = purchase_amt * 0.0775;
return sales_tax;
}
//add up a total for the purchase amount plus the sales tax for that amount
float add_total(float sales_tax, float purchase_amt)
{
total = sales_tax + purchase_amt;
return total;
}