General discussion

Locked

Anything wrong with this bit of code

By mckay_w ·
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')

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Anything wrong with this bit of code

by john_wills In reply to Anything wrong with this ...

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.

Collapse -

Anything wrong with this bit of code

by mckay_w In reply to Anything wrong with this ...

The question was auto-closed by TechRepublic

Collapse -

Anything wrong with this bit of code

by nikki96 In reply to Anything wrong with this ...

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

Collapse -

Anything wrong with this bit of code

by mckay_w In reply to Anything wrong with this ...

The question was auto-closed by TechRepublic

Collapse -

Anything wrong with this bit of code

by mckay_w In reply to Anything wrong with this ...

This question was auto closed due to inactivity

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums