Discussion Locked

Anything wrong with this bit of code

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Learning C
anything wrong with this code
no matter what i enter, the GST is set at 10%
char Description[100];
float Price;
char Type;
float GST;

printf("Enter Description: ");
scanf(" %s", Description);

printf("Enter Price: $");
scanf("%f", &Price);

printf("Enter Type: ");
scanf(" %c", &Type);

if (Type = 'a' || 'g' || 'o')
{
GST = Price / 10;
}
else
{
if (Type = 's')

5 replies

by john_wills

Well, give me a C lesson. Why does scanf's 2nd parameter start with an ampersand in two invocations but not the first? How do you know what you get in GST? What happens in the rest of the if nest? I suggest displaying GST together with price and type: you may just find that the results make sense.

by nikki96

Two equals for compare. One assigns.
And you can't nest compares like that.

if (Type == 'a' || Type == 'g' || Type == 'o')

if (Type == 's')

Maintain the float.
GST = Price / 10.0;

You have no error checking in place. This will crash if the user doesn't enter numbers for the numeric data. Or try to read it as hex or something equally odd, like convert it to the decimal equivalent. Don't just assign user input to a variable without checking what kind of data it is.

HTH

This discussion is closed to new replies.