![]() |
selection
Dear sir
I am write a program to ABC County (county code A) has a 7 percent sales tax rate; the rest of the state is 6 percent rate. I want to print out the amount owed on purchase including sales tax, but my output is displayed as below: Amount of Purchase? 100 County? Total Bill: -0.00 Do you know why??? #include <stdio.h> #include <conio.h> #include <ctype.h> void main() { float Purchase, TaxRate; char County; clrscr(); printf("Amount of purchase? "); scanf("%f", &Purchase); printf("County? "); scanf("%c", &County); if (toupper(County) == 'A') {TaxRate = Purchase * 0.07;} else if((County >='B') && (County <='Z')) {TaxRate = Purchase * 0.06;} else if (isdigit(County)) {printf("Invalid Character!");} printf("Total Bill: %.2f", TaxRate); getch(); } Thanks Annie |
Say fflush(stdin); after scanf("%f", &Purchase);
|
Sorry for correcting you, but fflush(stdin) can invoke undefined behaviour you can use while (getchar() != '\n'); instead. void main can also produce undefined behaviour so use int main() and put return 0; at the end of your program
About your program, this might help you a bit Code:
#include <stdio.h> |
Quote:
Second of all you better know what you are talking about before correcting someone. Using void main() is fine as long as it is part of the language of the particular compiler that you are using. And yes there is more than one C++ compiler, quite a few actually. Using void main() was how main was declared in C but in C++ depending on which compiler you use it can be declared as void main() or int main(). I imagine that Xiang is using a compiler that accepts void main() since obviously he has compiled and ran his program already but just didn't get the output he wanted. |
First of all sorry for the language, but it wasn't meant to be disrespectfull. I also have been corrected when I made a reply, and I felt that the person who corrected me was a , well you know. I didn't want him/her to feel the same way about me, that is why I said it. If you or him/her took it the wrong way, then I'm sorry, I apologize.
Second, using void main isn't right. Not because the compiler allows it, that doesn't mean it is right. C99 standard: Quote:
But "It shall be defined with a return type of int" is good enough for me. From Bjarne Stroustrup: Quote:
|
| All times are GMT +1. The time now is 12:24 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.