Thread: Problem in very simple code

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Problem in very simple code

    Hi, I am having a problem with this very simple piece of code that I am learning from a book.

    Code:
    #include <stdio.h>
    
    /*#define OVER_TWENTY_FINE 75*/               /*problem*/
    
    main()
    
    {
    
    int   fine = 5,
         number_of_a = 11,
         OVER_TWENTY_FINE = 75;                        /*solution*/
    
    
    if(number_of_a <= 10)
        
    	  ++fine;
    else
    	
         fine += OVER_TWENTY_FINE;
    	   
    
    printf("\nfine is %d." , fine);	
    
    }

    As you can see this is a simple "if" "else" decision. I originally had OVER_TWENTY_FINE in a "#define" statement before main(). Now I have it commented out, and have instead declared it as an integer. This is because when I was using it as a #define statement it wouldn't let the "if else" work. I would get these two errors:

    error C2059: syntax error : 'if'
    error C2181: illegal else without matching if

    After I commented it out and started declaring it as int, the code works fine. I can't see what could be wrong with this. Suggestions??.....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Works for me
    Code:
    #include <stdio.h>
    
    #define OVER_TWENTY_FINE 75
    
    int main()
    {
      int   fine = 5,
            number_of_a = 11;
    
      if(number_of_a <= 10)
        ++fine;
      else
        fine += OVER_TWENTY_FINE;
    
      printf("\nfine is %d." , fine);
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Maybe its my compiler?? Its microsoft visual c++ 6.0

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's extremely unlikely for vc6 to fail on something that simple.
    Post the exact code and the exact error messages.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Code:
    #include <stdio.h>
    
    #define OVER_TWENTY_FINE 75    /*culprit*/
    
    main()
    
    {
    
    int  fine = 5,
         number_of_a = 11,
         /*OVER_TWENTY_FINE = 75;*/        /*culprit*/
    
    
    
    
    if(number_of_a <= 10)
        
    	  ++fine;
    else
    	
         fine += OVER_TWENTY_FINE;
    	   
    
    
    
    printf("\nfine is %d." , fine);	
    
    }

    D:\Program Files\Microsoft Visual Studio\MyProjects\corr\corr.c(220) : error C2059: syntax error : 'if'

    D:\Program Files\Microsoft Visual Studio\MyProjects\corr\corr.c(223) : error C2181: illegal else without matching if

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Put a semicolon after the number_of_a = 11. The way you have it now will cause error, because you only end with ,

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well you can't have a #define and an int declaration with the same name, if that's what you're trying to do.

    The compiler will simply substitute the #define name with it's expansion, and result in a nonsense
    Code:
    int  fine = 5,
         number_of_a = 11,
         75 = 75;

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Nope. I was commenting them out back and forth while trying.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The reason the last code sample won't compile has nothing to do with the #define. It is because inappropriate material is commented out later.

    Effectively the compiler will see this as;
    Code:
    main()
    {
         int fine = 5, number_of_a = 11,      /*  comma, not semi-colon at end of line */
    
         if (number_of_a <= 10)
           /* etc   */
    }
    When it sees a comma at the end of the declarations, it expects more declarations. However, along comes a reserved keyword ......

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Ok, the code I posted above was a serious simplification of a larger program that I was doing.
    I THOUGHT I had isolated the problem, and thats what the above was. Not the case
    Sorry about that.

    This is the actual program:

    Code:
    #include <stdio.h>
    
    #define OVER_TEN_FINE 50
    /*#define OVER_TWENTY_FINE 75*/
    #define D_VIOLATIONS_OVER_3 20
    
    main()
    
    {
    		 
    	int  fine = 5,
    	
    		 number_of_a,
    		 number_of_b,
    		 number_of_c,
    		 number_of_d,
    		 
    		 a_charge = 10,
    		 b_charge = 20,
    		 c_charge = 30,
    		 d_charge = 50,
    
    		 total_number_fines = 0,
    		 
    		 total_number_of_A_fines = 0,
    		 total_number_of_B_fines = 0,
    		 total_number_of_C_fines = 0,
    		 OVER_TWENTY_FINE = 75,                                    
    		 number_of_d_over_three,
    		 USING_OVER_TWENTY_FINE = 0;
    		 
    		
    
      printf("\nPlease enter the number of A violations :");
      scanf("%d" , &number_of_a);
      total_number_fines += total_number_of_A_fines += number_of_a;       /*accumulating the total # of fines and a fines*/
      number_of_a *= a_charge;				              /* the number of times of violation multiplied by corresponding fine */	
    
      
      printf("\nPlease enter the number of B violations :");
      scanf("%d" , &number_of_b);
      total_number_fines += total_number_of_B_fines += number_of_b;
      number_of_b *= b_charge;	
    
    
      printf("\nPlease enter the number of C violations :");
      scanf("%d" , &number_of_c);
      total_number_fines += total_number_of_C_fines += number_of_c;
      number_of_c *= c_charge;
    
    
      printf("\nPlease enter the number of D violations :");
      scanf("%d" , &number_of_d);
      total_number_fines += number_of_d;							
    
    
      if(total_number_of_A_fines > 10) 
      
    	  number_of_a += OVER_TEN_FINE;
      
      if(total_number_of_B_fines > 10)				 /*<------- implementing the over 10 violations rule for*/		
      										       /*a, b, and c-------->*/
    	  number_of_b += OVER_TEN_FINE;
      
      if(total_number_of_C_fines > 10)
      
    	  number_of_c += OVER_TEN_FINE;
      
      
      
      if(total_number_fines > 20)      /*----a,b, and c only!----*/
      
    	  if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    
    	  {
    	        USING_OVER_TWENTY_FINE = OVER_TWENTY_FINE;        
    	    /*printf("\nusing over twenty fine is %d." , USING_OVER_TWENTY_FINE);*/ /*to check for assignment*/
    	  }
    	   
    	
    			
    		  
      	  
        if(number_of_d > 3)
    
      {
    	  
    	  number_of_d_over_three = number_of_d - 3;
      	  number_of_d = (3 * d_charge) + (number_of_d_over_three *= D_VIOLATIONS_OVER_3);
    				
      }
    
      else
    	
    	  number_of_d *= d_charge;
    
      printf("\nusing over twenty fine is %d." , USING_OVER_TWENTY_FINE);/*to check for assignment*/
      
      printf("\nnumber of d is %d." , number_of_d);
      printf("\nThe total fine for the person is %d." , USING_OVER_TWENTY_FINE + number_of_a + number_of_b + number_of_c + number_of_d);
      printf("\nThe total number of fines is %d." , total_number_fines);
    
    }
    So heres the thing. The nested "if" statement that starts with - if(total_number_fines > 20).

    You see that it says : if total number of fines is > than 20: and
    if number of a, b and c are <= to 10: then
    assign the value of OVER_TWENTY_FINE to
    USING_OVER_TWENTY_FINE.

    (the following printf is just to check to see if it made it)

    Well, the thing is it NEVER makes the assignment no matter what I do. I tested every other branch of the program and everything else works expectedly. But for some reason, USING_OVER_TWENTY_FINE wont take any value. I placed printf statements here and there to trace it and it never takes on a value. Regardless of whether OVER_TWENTY_FINE is a #define statement, or as an integer. So the rest of that nested if doesn't execute. I placed another printf statement down in the output to test the value of USING_OVER_TWENTY_FINE, still nothing.

    Thats the problem.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The only way the condition in;
    Code:
    if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    will be true will be if all the values you enter are negative. But that would make total_number_fines negative, which means the enclosing
    Code:
    if(total_number_fines > 20)
    will always fail. In other words, you have no way of getting to the inner block (if outer condition is true, the inner one is false and vice versa).

    I would also suggest you don't do tricks like;
    Code:
        a += b += c;
    Firstly, it probably doesn't do what you actually intend in this case. Second, such constructs that rely on side effects and order of multiple operations, tend to be confusing to people (including yourself) who have to maintain the code in future.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    *EDIT* Darn grumpy beat me by 2 mins, I spent 10 writing the explaination lol *EDIT*

    The problem is your conditional statement. It can NEVER get in there, simply because of what you do, lets start from the top:

    To get in there, I have to initially have over 20 in total_number_fines, which is accumlated from violation's a, b, and c. So lets say I put 10 in for each, and 5 for d (since it doesn't really matter).

    I now have 30 in total_number_fines at this point, which is over 20. So, we can surpase the first statement, but this is what I don't get:

    Code:
    number_of_a *= a_charge;
    number_of_b *= b_charge;
    number_of_c *= c_charge;
    Code:
    if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    You put these charges on each, and then supply this condition, before you're allowed to set USER_OVER_TWENTY_FINE? With the charges alone, you're never going to get in here, and satisfy the first condition

    Code:
    if(total_number_fines > 20)
    So if you simply remove
    Code:
    if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    Then your program works as I assume you want.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Grumpy,
    I think you might have the two ifs reversed. The
    Code:
    if(total_number_fines > 20)
    comes first, then the
    Code:
    if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    As a test, if I input 10, 5, 5, 3 for a, b, c, and d, total_number_fines will report "23" correctly in the ouput string. Then the following if says if a,b, and c arent "individually" less than or equal to 10, (thats what Im trying to make it say) execute the
    Code:
    USING_OVER_TWENTY_FINE = OVER_TWENTY_FINE;
    and that assignment just wont happen for some reason. I think maybe I do have the logic in the
    Code:
    if( (number_of_a <= 10) && (number_of_b <= 10) && (number_of_c <= 10) )
    wrong and thats why its not going through. Its supposed to be if the number of a, b, or c are "individually" <= 10. I thought about using || instead of &&. maybe?

  14. #14
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by richdb
    Its supposed to be if the number of a, b, or c are "individually" <= 10. I thought about using || instead of &&. maybe?
    if a OR b OR c is different than if a AND b AND c. So yes you should replace in that respect...

    but none of those are ever going to be less than 10, because of the fine charges you put on them before that.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Just so no one is confused, maybe I should have said earlier, in the input prompts at the beginning, the values "number_of_a" through d, are first being used to accept the users input in the scanf statements, then that value is being added to the total_number_of_*fines and total_number_fines, and then finally is multiplied by the fine for the corresponding letter (a_charge, b_charge, etc.) Sloppy coding I know. I should be using another variable like "final_a_charge" or something. Hope that clears any confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 06:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM