Thread: else/if error

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    40

    else/if error

    hey everyone,

    writing a little program and i'm getting an error that says 'else without a previous if'.

    Code:
        
    
    char ans_one, ans_two, ans_three, months_experience;
    
        printf("Have you previously worked in a game store? (Y/N)\n");
        scanf(" %c", &ans_one);
    
        printf("Do you have any experience in sales? (Y/N)\n");
        scanf(" %c", &ans_two);
    
        if (ans_two == 'y')
            printf("How many months of experience do you have? \n");
            scanf(" %c", &months_experience);
        else if (ans_two == 'n')
            printf("Do you like video games? (Y/N)\n");
            scanf(" %c", &ans_three);
        else if (ans_two == 'n')
            printf("Do you like video games? (Y/N)\n");
            scanf(" %c", &ans_three);
    am i leaving something out? do i need curly braces? the if is there; so i'm sure i've forgotten something..

    thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, you need curly braces. You need them whenever you have more than one statement that is part of an if/else, loop, etc. One way to avoid this is to always use them, even if you only have one statement. Also notice that both your else if statements have the same condition, so the second one will never execute.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    what are the correct places to put the braces?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Around all the stuff that belongs with that part of the if/else:
    Code:
    if (ans_two == 'y') {
        // everything you want the program to do if ans_two is 'y'
        printf("How many months...");
        scanf(...);
    }
    and similarly for the else if parts.

    EDIT: If your book/professor didn't cover this, then perhaps you should get a new one. If you don't have a book, check out our C Book Recommendations.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by jpatrick View Post
    what are the correct places to put the braces?
    No offense, but how can you possibly not know? Literally every working C program uses at least one pair of curly braces, and it's elementary syntax.

    Code:
    if (condition) {
        /* statements */
    }

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    I actually had it right but i had an extra curly brace that i didn't notice in there and i didn't notice so i just assumed i was doing it wrong =\

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    For every open curly brace you should indent your code from then on. For every close brace you should "out dent"... That's what makes programs readable. If there are any extra braces that do not correspond to the indent level then you should be able to spot that right away.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    Hey this is a different bit of code. but i'm having some issues with the math.

    Code:
    int main() {
        int num_hours_worked, pay_rate, employee_type, overtime_hours_worked;
        float weekly_pay_1, weekly_pay_2, weekly_pay_3, overtime_pay;
    
        printf("Are you and hourly employee (1) or a manager (0)?\n");
        scanf("%d", &employee_type);
    
        if (employee_type == 0) {
            printf("How many hours did you work this week?\n");
            scanf("%d", &num_hours_worked);
    
            printf("What is your hourly pay rate?\n");
            scanf("%d", &pay_rate);
    
            (weekly_pay_1) = (pay_rate * 50);
    
            printf("You will get paid $%f this week.\n", weekly_pay_1);}
    
        if (employee_type == 1) {
            printf("How many hours did you work this week?\n");
            scanf("%d", &num_hours_worked);
    
            printf("What is your hourly pay rate?\n");
            scanf("%d", &pay_rate);
    
            (weekly_pay_2 = (pay_rate * num_hours_worked));
    
            printf("You will get paid $%f this week.\n", weekly_pay_2);}
    
         else if (employee_type == 1 && num_hours_worked > 40) {
    
             (num_hours_worked % 40) == (overtime_hours_worked);
    
             (overtime_pay) = (pay_rate * 1.5) * (overtime_hours_worked);
    
             (weekly_pay_3) = (weekly_pay_2 + overtime_pay);
    
             printf("You will get paid %f this week.\n", weekly_pay_3);}
    
       return 0;
    }
    The math is almost always wrong. for example:

    the user inputs 0 for "manager" and the manager gets paid a set salary for his/her hourly pay regardless of how many hours are worked. its always hourly pay * 50 hours. if employees work over 40 hours a week, the hours past 40 are considered overtime for which they get paid 1.5* as much as their usual hourly rate.

    what did i do wrong?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is your understanding of if/else that is flawed. Read anduril's first reply in this thread.

    Code:
        if (employee_type == 1)
        {
            first_block();
        }
        else if (employee_type == 1 && num_hours_worked > 40)
        {
             second_block();
        }
    The point of if/else is that it tests alternatives in sequence, stops when it finds a match. In the above, once employee_type is found to be 1, the first_block will be executed and second_block() never will be.


    Also, if you have some code that is the same in every block (in your case, asking for the number of hours worked) then move that code to a point BEFORE the if/else testing. Don't replicate the code for every condition.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    maybe look at line 32,

    " == " is for equivalence

    " = " is for assignment

    also.. a familiar cry... initialise variables.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    (num_hours_worked % 40) == (overtime_hours_worked);
    What exactly do you think this statement does? Remember, a double == only compares two things, a single = will assign a value. You want to assign to overtime_hours_worked, so you want overtime_hours_worked = ??? Also, the % operator is the modulus or remainder operator. Talk it out, write it on paper, work some examples:
    If they worked 47 hours, how many overtime hours is that? How did you figure that out?
    What if they worked 53 hours? 87 hours? 100 hours?

    Also, your if/else stuff is off. all the parts of an if/else if series are mutually exclusive. If the employee type is 1, the if part is true, and the else if for overtime will never execute, even if hours_worked is more than 40. You need to make that a regular if inside the if (employee_type == 1) block:
    Code:
    // you only need one weekly_pay variable, because you never need to access all 3 at once
    if (employee_type == 0) {
        weekly_pay = pay_rate * 50
    }
    else if (employee_type == 1) {  // here else if is appropriate, employee can't be type 0 and 1 at the same time
        get hours worked
        if (hours_worked <= 40) {
            weekly_pay = pay_rate * hours_worked
        }
        else {
            overtime_hours_worked = ???
            weekly_pay = ???
        }
    }
    print "You will get paid..."  // move common code outside the if statements, to avoid duplication.

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i put all of this

    Code:
    printf("Are you and hourly employee (1) or a manager (0)?\n");
        scanf("%d", &employee_type);
    
        printf("How many hours did you work this week?\n");
        scanf("%d", &num_hours_worked);
    
        printf("What is your hourly pay rate?\n");
        scanf("%d", &pay_rate);
    above the if/else statement.

    i think i need the "==" here:
    Code:
    (num_hours_worked % 40) == (overtime_hours_worked);
    otherwise i get the error message '1value required as left operand of assignment'

    since the second block will never be executed, in what way should i change the code to involve the overtime pay? also.. any ideas as to why the math is coming up incorrectly for the first if statement?

    if i test this:
    Are you an hourly employee (1) or a manager(0)?
    0
    How many hours did you work this week?
    50
    What is your hourly pay rate?
    23.75

    the user is supposed to earn $1187.50 this week. 23.75*50 = 1187.50 but the answer comes back as 1150 every time.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jpatrick View Post
    i put all of this

    Code:
    printf("Are you and hourly employee (1) or a manager (0)?\n");
        scanf("%d", &employee_type);
    
        printf("How many hours did you work this week?\n");
        scanf("%d", &num_hours_worked);
    
        printf("What is your hourly pay rate?\n");
        scanf("%d", &pay_rate);
    above the if/else statement.
    Good. And you move the "You will get paid..." printf below the if/else, right?

    i think i need the "==" here:
    Code:
    (num_hours_worked % 40) == (overtime_hours_worked);
    otherwise i get the error message '1value required as left operand of assignment'
    No, that means that the left hand side is not something you can assign to. And it's a lvalue (as in lowercase L, not 1). Think of it as "location" value, i.e. there is a location where you can store the result. num_hours_worked has a location, but the expression "num_hours_worked % 40" does not. You need to assign to overtime_hours_worked:
    Code:
    overtime_hours_worked = // you fill in this expression
    since the second block will never be executed, in what way should i change the code to involve the overtime pay? also.. any ideas as to why the math is coming up incorrectly for the first if statement?
    I gave you the structure, refer to my post #11.

    if i test this:
    Are you an hourly employee (1) or a manager(0)?
    0
    How many hours did you work this week?
    50
    What is your hourly pay rate?
    23.75

    the user is supposed to earn $1187.50 this week. 23.75*50 = 1187.50 but the answer comes back as 1150 every time.
    [/quote]
    What type is pay_rate? Does that type support fractional parts (i.e. the 75 cents in 23.75)?

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    yes i now have
    Code:
    printf("You will get paid $%f this week.\n", weekly_pay);
    only once below the entire if/else statement. I now realize my boneheaded mistake of having pay_rate as an integer instead of a float since it is a decimal..

    i still am not understanding the overtime_hours_worked issue. am i not supposed to use the mod? how else can the program understand how many hours an employee has worked after his/her 40th work hour?

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i'm an idiot.. think i got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM