Problems with program verification. Please help!
// Guys I'm having trouble with my code and are ripping my hair out trying to resolve this. The problem is I am trying to test my code so that it does not calculate negative numbers. If anyone can help smooth out this program, I would really appreciate it. You are welcome.
// The purpose of the tax calculator program will be to use C programming to calculate sales tax for each Kudler Fine Food store (DelMar, Encinitas and La Jolla) // Standard I / O handling
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
//Starting point
int main ()
{
//Variables defined
//st = sales tax, tax rated = tr, pa = total purchase amount, sa = purchase amoount
float fstDelmar;
float ftrDelmar;
float fpaDelmar;
float fstEncinitas;
float ftrEncinitas;
float fpaEncinitas;
float fstLajolla;
float ftrLajolla;
float fpaLajolla;
float fsaPurchaseAmount;
int fStoreselect;
//Variable initializations for tax rates and purchase amount
ftrDelmar = .0725;
ftrEncinitas = .075;
ftrLajolla = .0775;
fsaPurchaseAmount = 0;
//Header & Introduction
printf("\n***********************************************************************");
printf("\n* Kudler Fine Foods *");
printf("\n* Sales Tax Calculator *");
printf("\n* Version 3.0 *");
printf("\n***********************************************************************\n");
//Ask user to select store.
printf ("\n STORE LOCATION \n");
printf ("\n 1) Del Mar \n");
printf ("\n 2) Encinitas \n");
printf ("\n 3) La Jolla \n");
printf ("\n Please select the store location (1-3): \n");
scanf ("%d", &fStoreselect);
//Validate store selection.
while (fStoreselect < 1 || fStoreselect > 3) {
fflush (fStoreselect);
printf ("INVALID SELECTION! Please select a valid location (1-3): \n");
scanf ("%d", &fStoreselect);
}
//Ask user to enter in total purchase amount.
printf ("\n What was your purchase amount? ");
scanf("$%f", &fsaPurchaseAmount); //user enters variable amount
//Validation to ensure that user enter in purchase amounts using correct format.
while (fsaPurchaseAmount <= 0.0)
{
fflush(fsaPurchaseAmount);
printf("\n INVALID SELECTION! Please enter a valid purchase amount greater than zero.\n");
printf("\n The purchase amount is: $ ");
scanf("%f", &fsaPurchaseAmount);
}
//Calculation of sales tax in dollars for each of the store locations
fstDelmar = fsaPurchaseAmount * ftrDelmar;
fstEncinitas = fsaPurchaseAmount * ftrEncinitas;
fstLajolla = fsaPurchaseAmount * ftrLajolla;
//Calculation of total sales amount for each of the locations
fpaDelmar = fsaPurchaseAmount + fstDelmar;
fpaEncinitas = fsaPurchaseAmount + fstEncinitas;
fpaLajolla = fsaPurchaseAmount + fstLajolla;
//Displaying sales amount for purchase for each of the different locations
switch (fStoreselect) {
case 1:
//for Delmar location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstDelmar, fpaDelmar);
break;
case 2:
//for Encinitas location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstEncinitas, fpaEncinitas);
break;
case 3:
//for La Jolla location
printf("\n Purchase Amount Sales Tax Total Sales Amount ");
printf("\n _______________ _________ _________________ ");
printf("\n $%.2f $%.2f $%.2f",fsaPurchaseAmount, fstLajolla, fpaLajolla);
break;
}
printf("\n Hit the ENTER key to exit program\n");
//Pause the screen and wait for user to hit the ENTER key
getchar();
//EOF
}
a source to share
Here are some notes on the presented code:
-
Don't use floating point money. It will only cause grief later on. Ideally, check with an accounting professional. In the meantime, do the arithmetic in cents and scale to and from dollars for display.
-
Calculations such as sales tax can be correctly performed with floating point multiplication, but make sure you follow good rounding practices to the nearest cent. Again, seek advice from an accounting professional.
-
fflush()
does not do what you think. For a file descriptor open for writing (for examplestdout
), it ensures that all output on that descriptor is complete. It is used after printing a prompt and before calling something likescanf()
to read input. Example:printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin);
-
Always check the return value
scanf()
. It returns the number of conversions that have succeeded. If it does not match the number of format specifiers, then only those that succeed have the values ββrecorded for the named variables. -
Be careful with literal characters other than spaces that appear in the format string
scanf()
. They must match exactly with the input text, or the conversion fails. Thus, a format like this"$%f"
only matches an input signal that includes an alphabetic dollar sign. This is probably not what your user expects. The format is"%f"
easier for the user. If you want to allow an additional dollar sign, thisscanf()
may not be the best choice.
a source to share