Thread: Another Novice question..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Another Novice question..

    I am sure this will be obvious to most. But I keep getting an error message on my code below. I am new to C, so any help or advice will be much appreciated !!

    Code:
    # include <stdio.h>
    
    int main () {
    
    int age;
    int price = 20;
    int concessionprice;
    
      printf("Enter your age?: ");
      scanf("%d", &age);
    
      if (age >= 18)
      printf("Please pay $%d.\n", price);
      else if (age > 12 && age < 18)
      concessionprice = age/2;
      printf("Please pay $%d.\n", concessionprice);
      else 
      printf("children under 13 are free");
    }


    The error message I get is:

    Code:
    17:3: error: expected expression
      else
      ^
    1 error generated.
    make: *** [testinif] Error 1


    I don't understand what expression is needed after Else?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by Paradigm View Post
    I am sure this will be obvious to most. But I keep getting an error message on my code below. I am new to C, so any help or advice will be much appreciated !!

    Code:
    # include <stdio.h>
    
    int main () {
    
    int age;
    int price = 20;
    int concessionprice;
    
      printf("Enter your age?: ");
      scanf("%d", &age);
    
      if (age >= 18)
      printf("Please pay $%d.\n", price);
      else if (age > 12 && age < 18)
      concessionprice = age/2;
      printf("Please pay $%d.\n", concessionprice);
      else 
      printf("children under 13 are free");
    }


    The error message I get is:

    Code:
    17:3: error: expected expression
      else
      ^
    1 error generated.
    make: *** [testinif] Error 1


    I don't understand what expression is needed after Else?
    Try this attached code instead. U were missing a couple of braces in the else if block.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Problem solved. Thanks alot!

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    U r welcome..But keep i mind that u shouldn't read too much into the errors of the compiler..they r usually not what they look like...when it says something is wrong with a particular line, it usually means there is some error somewhere in the code and not in that line..so u need to go through the whole code. Thats the reason why we have so many debugging tools inspite of compiler giving all the errors. I too am a beginner and had to learn all this the hard way..So happy learning.Good luck!!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by livin View Post
    U r welcome..But keep i mind that u shouldn't read too much into the errors of the compiler..they r usually not what they look like...when it says something is wrong with a particular line, it usually means there is some error somewhere in the code and not in that line..so u need to go through the whole code. Thats the reason why we have so many debugging tools inspite of compiler giving all the errors. I too am a beginner and had to learn all this the hard way..So happy learning.Good luck!!
    There is a reason compilers list errors... and it's not so you can ignore them.
    And yes, the error is almost always on the line number indicated.

    Debugging tools exist because "it compiles" does not imply "it works".

    Finally... see this Forum's homework policy...
    C Board - Announcements in Forum : General Programming Boards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. A novice programming question...
    By curious in forum C Programming
    Replies: 8
    Last Post: 11-12-2008, 06:09 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM