tags:

views:

159

answers:

2

//Guys I have issues with my code and have been tearing my hair apart trying to resolve this. THe issue is that I am trying to validate my code so it doesn't calculate for negative numbers. If any one can help smooth up this program I would really appreciate it. Please.

//The objective of the tax calculator program will be to use C programming to calcualte sales tax for each of the Kudler Fine Food stores (DelMar, Encinitas and La Jolla) //Standard input/output processing

#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's 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      
}
+2  A: 

This line has bug:

scanf("$%f", &fsaPurchaseAmount); //user enters variable amount

it supposed to be:

scanf("%f", &fsaPurchaseAmount); //user enters variable amount

Thanks much! I think this resolved the issue I had. Who new such a small thing bought me major problems.
Sara
+1  A: 

Here are some observation about the code as presented:

  • Don't use floating point for money. It will only cause grief later. Ideally, take advice from an accounting professional. In the mean time, do arithmetic in cents, and scale to and from dollars for display.

  • Calculations like sales tax might be correctly done with a floating point multiply, but make sure you comply with the accepted practices for rounding back to whole cents. Again, seek advice from an accounting professional.

  • fflush() doesn't do what you think it does. For a file descriptor opened for writing (such as stdout) it guarantees that all output on that descriptor has been completed. It is used after printing a prompt and before calling something like scanf() to read the input. An example is: printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin);

  • Always check the return value of scanf(). It returns the number of conversions that succeeded. If it doesn't match the number of format specifiers, then only those that succeeded had values written to the named variables.

  • Be wary of literal characters other than whitespace appearing in a scanf() format string. These must be matched exactly by the input text, or the conversion fails. So a format like "$%f" is only matched by input that includes a literal dollar sign. This is probably not what your user expects. The format "%f" is easier on the user. If you want to allow an optional dollar sign, then scanf() may not be the best choice.

RBerteig
Got it. Thanks much for the advice and help. I think I have it down now.
Sara