Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. 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
Code:
```#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main()   //allways use int main()
{
float Purchase, TaxRate;
char County;

clrscr();
printf("Amount of purchase? ");
scanf("%f", &Purchase);
while (getchar() != '\n'); //clear the inputbuffer
printf("County? ");
scanf("%c", &County);

County=toupper(County); //store the upper case value ov County in County for later use

if ( 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();
return 0; //return a value to the OS
}```

• Originally posted by maes
I don't want to be an ******* 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
Well first of all watch your language in our forums.

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:
5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.
I agree that it doesn't explicitly say: "you can not use void main()"
But "It shall be defined with a return type of int" is good enough for me.

From Bjarne Stroustrup:
The definition
void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts
int main() { /* ... */ }

and
int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•