# Thread: "Do While" loop

1. ## "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!!

Code:
```
/*
Program: Kudler Tax Calculation Program
SR-kf-008
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

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;
break;
case 2:
fTax = fPurchaseAmount  * fEncinitas;
break;
case 3:
fTax = fPurchaseAmount  * fLaJolla;
break;
}

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");
fflush(stdin);
getchar();
}```

2. What is the deal? We won't code for you, we can help you. Where specifically in this source is your problem?

3. > void main()
main returns an int, see the FAQ
http://faq.cprogramming.com/cgi-bin/...&id=1043284376

> fflush(stdin);
Must be your lucky day, this is in the FAQ as well (hint, you shouldn't do it)
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

> 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.
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
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.

Popular pages Recent additions