Thread: having problem with if statement

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    23

    having problem with if statement

    hello

    I have to show the purchase price, county and taxrate of that county and purchase including the tax

    Code:
    #include <stdio.h>
    float Purchase, TaxRate;
    int County;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%f", &Purchase);
     printf("COUNTY? ");
     scanf("%i", &County);
     if(County == 65)
        TaxRate = 7;
     else
        TaxRate = 6;
     printf("TOTAL BILL: $ ");
     if(County 65)                   //im having problems here
      printf("%3.2f", Purchase += (Purchase * TaxRate / 100));
     else
       printf("%3.2f", Purchase += (Purchase * TaxRate / 100));
     }
    the problem starts at the if statement,
    I put in the cost of 15 dollars
    then, A as that is the tax code for that county, this is supposed to choose the tax rate of 7
    if not A then the tax rate has to be 6

    I think it has something to do with the way im comparing values. I scanf for county, this is an int and would be a letter. I haven't been able to show upper or lower case. I thought that adding the decimal value 65 for A 97 for a, then it would choose that branch. this doesn't work though as when I put in either B or b it still uses the wrong value.
    not sure how to do this. I have looked at char but that isn't a part of this program task in the book. I have three variables and it doesn't state what value they are.
    county
    purchase and
    taxrate

    any help would be great
    the program is to make use of if, else if statements

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You simply have a syntax error on this line:
    Code:
    if(County 65)
    You did get it right on this line:
    Code:
    if(County == 65)
    Since you do the same thing on both branches of the if statement, you might as well just write:
    Code:
    printf("%3.2f", Purchase += (Purchase * TaxRate / 100));
    Personally, I would write:
    Code:
    Purchase += Purchase * TaxRate / 100;
    printf("%3.2f", Purchase);
    as it is less likely to result in a mistake.

    Quote Originally Posted by ninjaman
    I thought that adding the decimal value 65 for A 97 for a
    Avoid magic numbers. Instead of using 65, use 'A'. Instead of using 97, use 'a'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    Instead of using 65, use 'A'. Instead of using 97, use 'a'.
    What makes you suggest it? It is not that user presses 'A' when asked for county. He will have to enter numbered code of the county, I suppose, seeing the code. So why do you suppose this numbered code has anything to do with ASCII?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What makes you suggest it? It is not that user presses 'A' when asked for county. He will have to enter numbered code of the county, I suppose, seeing the code. So why do you suppose this numbered code has anything to do with ASCII?
    O_o

    would be a letter
    the decimal value 65 for A 97 for a
    I put in either B or b
    I have looked at char
    I suppose it could just be coincidence.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    May 2014
    Posts
    23
    hello

    I have changed the if to a switch case though im still having problems.

    Code:
    #include <stdio.h>
    int County, Purchase, TaxRate;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%i", &Purchase);
     printf("COUNTY? ");
     scanf("%i", &County);
     printf("TOTAL BILL: $ ");
     switch(County)
     {
     case'a':
     case'A':
        TaxRate = 7;
        Purchase += (Purchase * TaxRate / 100);
        break;
     case'b':
     case'B':
        TaxRate = 6;
        Purchase += (Purchase * TaxRate / 100);
        break;
     }
     printf("%3.2f", Purchase);
    }
    I cant get the switch case working for some reason. and I cant see the value at the end it just shows $ 0.00
    im reading a book called "ansi c" by steven c lawlor, its old but I cant imagine its changed much.
    any help or hint would be great.
    thanks
    simon

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try dividing by 100.0 instead of 100 I suggest learning about integer division.

    Edit: This line implies Purchase is a float/double; but, I think you declared it as int.
    Code:
    printf("%3.2f", Purchase);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I cant get the switch case working for some reason.
    O_o

    1): You are consuming, from input, an `int' while you are probably typing 'A'/'a'/'B'/'b' which are not of `int' type leaving the value of `Country' at zero.

    2): You have told `printf' that the variable `Purchase' is a `float' which is actually of `int' type.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    Code:
    #include <stdio.h>
    float Purchase, TaxRate;
    int County;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%f", &Purchase);
     printf("COUNTY? ");
     scanf("%i", &County);
     if(County == 65)
        TaxRate = 7;
     else
        TaxRate = 6;
     printf("TOTAL BILL: $ ");
     if(County == 65)                   //im having problems here
      printf("%3.2f",(float) Purchase += Purchase * TaxRate / 100);
     else
       printf("%3.2f",(float) Purchase += Purchase * TaxRate / 100);
     }
    use type casting

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    if you want to scan an ascii you have to use char
    Code:
    #include<conio.h>
    
    
    float Purchase, TaxRate;
    char County;
    
    
    int main()
    {
    	printf("AMOUNT OF PURCHASE? ");
    	scanf("%f\n", &Purchase);
    	printf("COUNTY? ");
    	scanf("%c\n", &County);
    	switch(County)
    	{
    		case 65:	TaxRate = 7;
    		   			Purchase += (Purchase * TaxRate / 100);
    	    			break;
    	 	default:	TaxRate = 6;
    				    Purchase += (Purchase * TaxRate / 100);
    				    break;
    	}
    	printf("%3.2f", Purchase);
    }

  10. #10
    Registered User
    Join Date
    May 2014
    Posts
    23
    I have tried the char thing and that doesn't work, it goes straight past scanf("%c", &County)
    this part of the program is not working at all, it doesn't respond to a, A, 65 or 97. the TOTAL BILL is always 15.00 for a purchase of 15. no tax is added. so the switch is not working.
    any help getting the switch working. I have tried changing the float, int and char values. this has no effect.
    im not sure what is wrong here.

    thanks
    simon

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2014
    Posts
    23
    Code:
    #include <stdio.h>
    int Purchase, TaxRate;
    char County;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%i", &Purchase);
     printf("COUNTY? ");
     scanf("%c \n", &County);
     printf("TOTAL BILL: $ ");
     switch(County)
     {
     case 65:
        TaxRate = 7;
        Purchase += (Purchase * TaxRate / 100.0);
        break;
     default:
        TaxRate = 6;
        Purchase += (Purchase * TaxRate / 100.0);
        break;
     }
     printf("%3.2f", (float) Purchase);
    }
    this is the current code, I have made changes that people have suggested. I type in 15, A for county, and total bill appears as 15.00. when I type anything for county total bill comes up as 15.00.
    I don't think the switch case is working, or if its getting to switch case. I think it is bypassing it for some reason.

    thanks
    simon


    I included,
    printf("taxrate is %1 \n", TaxRate);
    and it prints,
    taxrate is 6

    so the switch case is working to some extent. I don't think the "County" thing is working. also, the expression purchase += (purchase etc..) this is not working. otherwise the default value would give a different number at output.

    ok latest code

    Code:
    #include <stdio.h>
    float Purchase, TaxRate;
    char County;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%f", &Purchase);
     printf("COUNTY? ");
     scanf("%c \n", &County);
     switch(County)
     {
     case 65:
        TaxRate = 7;
        break;
     default:
        TaxRate = 6;
        break;
     }
    Purchase += (Purchase * TaxRate / 100.0);
     printf("taxrate is %1.0f \n", TaxRate);
     printf("TOTAL BILL: $ ");
     printf("%3.2f", Purchase);
    }
    this takes and shows input, shows county, shows default taxrate no matter what letter is pressed, shows the correct value of purchase and tax. so the purchase expression is working. the switch case is not.
    Last edited by ninjaman; 07-20-2014 at 05:09 AM. Reason: something new

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your latest code looks like it is on the right track, but I would improve it to:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float Purchase, TaxRate;
        char County;
        printf("AMOUNT OF PURCHASE? ");
        scanf("%f", &Purchase);
        printf("COUNTY? ");
        scanf(" %c", &County);
        switch (County)
        {
        case 'A':
            TaxRate = 7;
            break;
        default:
            TaxRate = 6;
            break;
        }
        Purchase += (Purchase * TaxRate / 100.0);
        printf("taxrate is %1.0f\n", TaxRate);
        printf("TOTAL BILL: $ %3.2f\n", Purchase);
        return 0;
    }
    A few changes to note:
    • My indentation for a single level is more than just a space, hence it provides better contrast between indent levels.
    • I made the variables into local variables. It does not matter for so trivial a program, but you should get into the habit.
    • I use " %c" instead of "%c \n" for the format specification to read into Country. You want to discard the newline left over from the previous scanf, so it makes sense for there to be a leading space.
    • I use 'A' instead of 65.
    • I used a single printf for the TOTAL BILL printing. This, after all, is partly why we have formatted output. I could have combined it further with the taxrate printing.
    • I also used int main(void) instead of int main(). This makes no difference since this is a function definition, not a forward declaration, but you might as well get into the habit.
    • I placed a return 0; at the end. This is optional if you are compiling with respect to C99 or later as a special concession to the main function.

    Another thing that I could have done is to check the return value of scanf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2014
    Posts
    23

    all done

    Code:
    #include <stdio.h>
    float Purchase, TaxRate;
    char County;
    int main()
    {
     printf("AMOUNT OF PURCHASE? ");
     scanf("%f", &Purchase);
     printf("COUNTY? ");
     scanf(" %c", &County);
     printf("purchase %3.2f \n", Purchase);
     printf("county %c\n", County);
     switch(County)
     {
     case 'A':
        TaxRate = 7;
        break;
     case 'B':
        TaxRate = 6;
        break;
     default:
        printf("still not working \n");
     }
    Purchase += (Purchase * TaxRate / 100.0);
     printf("taxrate is %1.0f \n", TaxRate);
     printf("TOTAL BILL: $ ");
     printf("%3.2f \n", Purchase);
     printf("county is %c ", County);
    }

    the problem was with whitespace. I used "%c" instead of " %c". it has something to do with left over white space. so adding a whitespace sorted the problem.
    HORRAY

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with IF statement!
    By turkadurka in forum C++ Programming
    Replies: 8
    Last Post: 08-08-2012, 06:47 AM
  2. Problem with if statement
    By adohertyd in forum C Programming
    Replies: 4
    Last Post: 12-20-2011, 09:34 PM
  3. if statement problem.....
    By JM1082 in forum C++ Programming
    Replies: 16
    Last Post: 09-09-2011, 11:23 AM
  4. Problem with if statement
    By Sharifhs in forum C Programming
    Replies: 3
    Last Post: 08-11-2010, 05:02 PM
  5. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM