Thread: "Do While" loop

  1. #1
    Registered User
    Join Date
    Aug 2006

    "Do While" loop

    I'm trying to modify the program. So, the user inputs the purchase
    amount and the program checks the users input for validity. HELP I need today!!

    Program: Kudler Tax Calculation Program
    Date: August 11, 2006 
    Author: Douglas Niemczynski 
    #include <stdio.h> 
    void main()
        float fDelMar = 0.0725f;              // tax for Del Mar
        float fEncinitas = 0.0750f;         // tax for Encinitas
        float fLaJolla = 0.0775f;           // tax for La Jolla
        float fPurchaseAmount = 0.0f;       // amount of purchase
        float fTax = 0.0f;                          // taxes
        float fTotal = 0.0f;
        int iStoreChoice = 0;               // menu selection
        // print menu
        printf("\n1 - Del Mar");
        printf("\n2 - Encinitas");
        printf("\n3 - La Jolla");
        printf ("\nSelect city number 1, 2, or 3\n");
        printf("\nEnter your selection: ");
        scanf("%d", &iStoreChoice);
        printf("\n\nEnter Purchase Amount: ");
        scanf("%f", &fPurchaseAmount);
        // apply taxes for purchase ammount
        switch (iStoreChoice)
        case 1:
            fTax = fPurchaseAmount * fDelMar;
        case 2:
            fTax = fPurchaseAmount  * fEncinitas;
        case 3:
            fTax = fPurchaseAmount  * fLaJolla;
        fTotal = fPurchaseAmount + fTax;
        // print out
        printf("Purchase Price:\t%.2f", fPurchaseAmount);
        printf("\nTotal price after taxes of (%.2f) is %.2f\n", fTax, fTotal);  
        printf("\nPress ENTER to Quit\n");

  2. #2
    Registered User
    Join Date
    Jun 2004
    What is the deal? We won't code for you, we can help you. Where specifically in this source is your problem?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > void main()
    main returns an int, see the FAQ

    > fflush(stdin);
    Must be your lucky day, this is in the FAQ as well (hint, you shouldn't do it)

    > So, the user inputs the purchase
    > amount and the program checks the users input for validity
    So you want to stop the code from blowing up if someone types in "abc" and you were expecting 123
    Erm, this is in the FAQ as well.
    Basically, you start by reading a line using fgets()
    Then you parse that using atoi(), sscanf(), strtol() or whatever else takes your fancy.
    Arrange a loop around this idea to keep going round until you get a successful READ and CONVERSION.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM