Thread: syntax error in my if statement

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    syntax error in my if statement

    Code:
    if (tax == 1) {
                price += shippingcost;   
                total = price - (price * TAX_RATE);
                return total;
           }
    I dont know why but its giving me the message "syntax error before '=' token
    for the line total = price - (price * TAX_RATE);. I cant figure it out why am I getting this error? (it is an 80 lined program and commenting this one line out allows it to compile).Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are all the variables defined?

    Also, are you really sure you want to be subtracting the taxes?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    What do you mean? I use that same variable total in another if statement right after this one.
    Code:
    if (tax == 1) {
                price += shippingcost;   
                total = price - (price * TAX_RATE);
                return total;
           }
        
           else if (tax == 0){
                total = price + shippingcost;
                return total;
           }
    The use of total in the else if statement doesn't produce any error.

    Oh I see what you mean about the taxes lol I changed it to
    Code:
    total = price + (price * TAX_RATE);
    but it does nothing to fix the compiling error.
    Last edited by UCFuser; 04-01-2011 at 08:50 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by UCFuser View Post
    What do you mean? I use that same variable total in another if statement right after this one.
    Code:
    if (tax == 1) {
                price += shippingcost;   
                total = price - (price * TAX_RATE);
                return total;
           }
        
           else if (tax == 0){
                total = price + shippingcost;
                return total;
           }
    The use of total in the else if statement doesn't produce any error.

    Oh I see what you mean about the taxes lol I changed it to
    Code:
    total = price + (price * TAX_RATE);
    but it does nothing to fix the compiling error.
    If it's not too long, post your whole code... or at least enough that we can see the variable declarations and the lines preceding what you've already provided. The error you're getting can also be caused by bracketing errors in earlier code...

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How do you define TAX_RATE?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Good idea... bracketing issue. Or bizarre macros that look like normal code. Or 'shippingcost' is a function or whatever. Or there is a garbagey character in the source/text file that's not visible.

    Yeah, Bayint Naung. TAX_RATE could be a #define with unbalanced parenthesis.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    This is the whole function

    Code:
    double itemshipping(double price, double weight, int quantity, int tax) {
         
         //declare some intitail variables.    
         double shippingweight, shippingcost, total;
         
         //calculate original cost before shipping costs are added. 
         //find out total cost of shipping. 
         price *= quantity;
         shippingweight = weight * (double)quantity;
        
    
         //setup logic statements to recognize how much to charge per pound for shipping
         if (weight <10)
              shippingcost = shippingweight * 4.00;
           
           else if (weight >=10 && weight <30)
                shippingcost = shippingweight * 2.5;
           
           else if (weight >=30 && weight <50)
                shippingcost = shippingweight * 1.5;
           
           else 
                shippingcost = shippingweight *.99;
        
           //setup logic statements to recognize whether 
           //an item is taxed or not and apply this to 
           //the final cost with the cost of shipping added in. 
           if (tax == 1) {
                price += shippingcost;   
                //total = price + (price * TAX_RATE);
                return total;
           }
        
           else if (tax == 0){
                total = price + shippingcost;
                return total;
           }
    }
    This is the whole function

    this is how TAX_RATE is defined
    Code:
    #define TAX_RATE = 0.065
    Also if it were a bracket error shouldn't I still get an error message when trying to compile after commenting out the bad line?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The macro is indeed the problem. You should have written:
    Code:
    #define TAX_RATE 0.065
    Otherwise, after macro replacement, your code actually looks like this:
    Code:
    total = price + (price * = 0.065);
    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

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Ahh I see what you mean, thanks!

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
                //total = price + (price * TAX_RATE);
    If there is no space between * and TAX_RATE.
    It will become
    total = price + ( price *= 0.065);
    Which is valid no compiler error. But undefined behaviour!!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bayint Naung
    If there is no space between * and TAX_RATE.
    It will become
    total = price + ( price *= 0.065);
    Which is valid no compiler error. But undefined behaviour!!
    My experiments show that that is not correct: I do get a compiler error, meaning that the separate tokens were not combined into one token.
    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
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Bayint Naung said IF there is no space. IF the programmer had used (price *TAX_RATE);

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, laserlight, you are right. I've compiled with gcc and I did get compiler error.
    But using -traditional-cpp flag, I was able to compile. Currently I could not find what the standard says about it.
    But most likely I might be wrong.
    Probably Adak turbo C should be able to compile cleanly.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I get results like laserlight. I can do
    Code:
    total = price + ( price *= 0.065);
    And I get the following warning:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:9: warning: operation on ‘price’ may be undefined
    But if I do
    Code:
    #define TAX_RATE = 0.065
    ...
    total = price + (price *TAX_RATE);
    I get the following:
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:9: error: expected expression before ‘=’ token
    
    $ gcc -E foo.c
    ...
    int main(void)
    {
        double total, price = 1.1;
    
        total = price + (price * = 0.065);
    Notice that the preprocessor inserts a space between * and TAX_RATE . I seem to remember reading this somewhere, but I don't know if it's in the GCC docs or standard somewhere. I believe this is done on purpose, to avoid exactly the problem we're discussing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. Pointer syntax in if statement
    By fkheng in forum C Programming
    Replies: 5
    Last Post: 06-10-2003, 04:01 AM
  3. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM