Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Need C Programming Help: When I run this code it prints the error statement no matter what the number is.?

printf("Please enter the percentage of tax for the low bracket.\n");

scanf("%lf", &low_percent);

if(low_percent > 100 || low_percent < 0);

{

printf("Error: Your number is larger than 100 or negative.\n\n");

system("pause");

return 0;

}

Update:

For example the user could enter in "30" and the program would still say "Error: Your number is larger than 100 or negative." What am I doing wrong?

Update 2:

I printed out the variable before the If statement and it's the same as what I entered. I then tried adding 5 to the variable before the If statement and it came out to what you'd expect, the original variable I entered, plus 5. Any other ideas? I think it's a problem with my if statment since scanf seems to be working fine.

Update 3:

Changing it to float didn't do anything. I decided to add the ".0" after the numbers in my If statement, but that didn't work either.

Update 4:

Ah ha! You had it #2. It was the semi-colon after the condition lol. Many thanks.

3 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    try this corection

    make sure the low_percent is declared float

    use %f as format specifier

    and remove the semicolon after your condition

    float low_percent;

    scanf("%f", &low_percent);

    if(low_percent > 100.0 || low_percent < 0.0)

  • Girl
    Lv 5
    1 decade ago

    Your scanf may not br formatted properly. In that case, output what low_percent is before the if statement.

    Otherwise, I'd think your low_percent is not an integer. Check its type by doing something that is restricted to numbers, like temp = low_percent + 5. If it is a non-number type, this should raise an exception.

    For example, in Python:

    >>> "30" > 100

    True

  • 1 decade ago

    3 .... was writing an answer before reading all the details... while writing the answer eyes fell on the last lines about #2 ... o yes. thumbing up #2 ... but still writing an answer ... :D

Still have questions? Get your answers by asking now.