Thread: I need some help !

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    I need some help !

    Right now this C program calculates the sales tax amount for each store location for a purchase amount of $125.00.
    [/CODE]
    #include <stdio

    int main()
    {
    int iSelection = 0;
    float fAmount = 125.00;//Purchased amount in US$
    float fTax1 = .0725;//Del Mar CA.
    float fTax2 = .0750;//Encinitas CA.
    float fTax3 = .0775;//La Jolla CA.

    printf("\n\tSelect your store location\n");
    printf("1\tDel Mar\n");
    printf("2\tEncinitas\n");
    printf("3\tLa Jolla\n");
    scanf("%d", &iSelection);

    //This calculates the tax rate for Del Mar CA.
    if (iSelection == 1) {
    printf("\nYour tax amount is: $%.2f\n", fAmount * fTax1);
    printf("\nClick the X in the upper right corner to exit this application.\n");
    } //end if

    //This calculates the tax rate for Encinitas CA.
    if (iSelection == 2) {
    printf("\nYour tax amount is: $%.2f\n", fAmount * fTax2);
    printf("\nClick the X in the upper right corner to exit this application.\n");
    } //end if

    //This calculates the tax rate for La Jolla CA.
    if (iSelection == 3) {
    printf("\nYour tax amount is: $%.2f\n", fAmount * fTax3);
    printf("\nClick the X in the upper right corner to exit this application.\n");
    } //end if
    [CODE]

    getch();
    return 0;


    I need it modified so that the user inputs the purchase amount, and the program checks the user input for validity. In addition to calculating the sales tax for each location, calculate and display the total sale amount for each location.

    Thanks
    alwalw

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Nice job on the code tags

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    looks like a beginner program. You already seem to know how
    to use scanf, so that should do for reading in the purchase
    amount:

    Code:
    scanf ("%f", &fAmount);
    here's a link to the FAQ on validation. this may be a bit complex
    to look at for a beginner, but its manageable if you stick with it.

    if you just want to check that the amount entered is not too big
    or too small (i.e. negative), try an if statement like this:

    Code:
    if (fAmount < 0 || fAmount > 5000)
    {
         printf ("Input is out of range\n");
    }
    finally, you already know how to do the calculations, so remove
    the selection part and the associated if statements. to calculate
    the total sale price, just add the tax on like this:

    Code:
    printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1)));
    now to finish up, in your code you use getch (), which is defined
    in the nonstandard header conio.h - you didn't include it in the
    post. also, its a bit much including a header for only one function,
    especially when the same effect can be achieved using getchar
    from stdio.h, take a look at this

    lastly, use indentation as it makes debugging code a little easier.
    as already mentioned, you used the code tags wrong, the top
    one should have been at the end of the code and vice versa
    that should help a little i think
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    'switch' statments would be more usefull for this application as well

    ssharish2005

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Ok now I'm a newbie so yu guys will have to break things down a bit for me..

  6. #6
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by alwalw
    Ok now I'm a newbie so yu guys will have to break things down a bit for me..
    Well, we don't HAVE to break them down, but we will because we're nice

    A switch statement

    Code:
    switch (iSelection) {
        case 1:
            // do something
            break;
        case 2:
            // do something
            break;
         default:
             // do the default thing 
             break;
    }
    can often replace multiple if statements like you have. If the range of iSelection is small and compact, some compilers will actually create befter perfroming code.

    But really, a switch in this instance would be easier to read.

    There are some restrictions to switch that I will leave up to you to figure out, but that's pretty much what it will do for you.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thanks !

Popular pages Recent additions subscribe to a feed