How do I use else if

I'm making a simple program to calculate worker pay but the else is having an error saying it's expecting a statement. The program requires user to input their worker type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "pch.h"
#include<iostream>
using namespace std;

int main()
{
	float worker_type, monthly_paid, daily_wages, salesperson, monthly_salary, hours_worked,gross_sales;
	cout << "---Program To Find Pay of a Worker---" << endl;
	cout << "\nEnter Type of Worker:";
	cin >> worker_type;
	if (worker_type = monthly_paid)
		cout << "\nEnter your monthly salary";
		cin >> monthly_salary;
	cout << "\nYour pay is RM" << (monthly_salary);
	 else if (worker_type = daily_wages)
		cout << "\nEnter total hours worked";
		cin >> hours_worked;
	cout << "\nYour pay is RM" << (hours_worked * 25);
	else if (worker_type = salesperson)
		cout << "\nEnter gross computer sales";
	cin >> gross_sales;
	cout << "\nYour pay is RM" << (500 + (0.05 * gross_sales));
	else if
		cout << "\n Error: unknown worker type";

		
	return 0;
}


edit: Thanks, I'll do that from now on. Still new to this :)
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What you've written is the equivalent of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (worker_type = monthly_paid)
{
  cout << "\nEnter your monthly salary";
}
cin >> monthly_salary;
cout << "\nYour pay is RM" << (monthly_salary);
else if (worker_type = daily_wages)
{
  cout << "\nEnter total hours worked";
}
cin >> hours_worked;
cout << "\nYour pay is RM" << (hours_worked * 25);
else if (worker_type = salesperson)
{
  cout << "\nEnter gross computer sales";
}
cin >> gross_sales;
cout << "\nYour pay is RM" << (500 + (0.05 * gross_sales));
else if
{
  cout << "\n Error: unknown worker type";
}

Do you see now why you're getting error messages?
Is it my codes are not within the curly brackets?
The curly braces just make your intentions more explicit.

What's missing from the last else if ?

Or perhaps you meant else unconditionally.
Ryan, "else if" must immediately follow "if".

You can't have
1
2
3
4
5
6
7
8
9
if (thing)
{
    do_stuff();
}
go_to_the_moon();
else if (other_thing)
{
    do_other_stuff();
}


You must have
1
2
3
4
5
6
7
8
if (thing)
{
    do_stuff();
}
else if (other_thing)
{
    do_other_stuff();
}

And refactor the code to put "go_to_the_moon();" somewhere else, where it logical should belong.
Ryan15 wrote:
Is it my codes are not within the curly brackets?

Yes, exactly.

salem c wrote:
The curly braces just make your intentions more explicit.

This is nonsense. The curly braces group together the statements that you want to be executed conditionally, into something that is considered a single, compound statement that follows the "if" statement.

Salem C, there's no polite way to say this: if you are this ignorant about the very basics of the language, you shouldn't be giving advice to other users, because you're only going to be confusing them by telling them things that are wildly incorrect.
Thanks a lot. Putting the code in the curly brackets did solve the problem.
Is there something I can type in the () that will cause the program to print out the error message if the user did not type in one of the three specified worker type?

1
2
3
4
else if ()
	{
		cout << "\n Error: unknown worker type";
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cctype>
#include <iomanip>

int main()
{
    std::cout << "---Program To Find Pay of a Worker---\n"
              << std::fixed << std::setprecision(2) ;

    std::cout << "\nEnter Type of Worker "
              << "(M for monthly paid, D for daily wages or S for sales person)\n"
              << "M/D/S? " ;

    char worker_type ;
    std::cin >> worker_type ;
    worker_type = std::toupper(worker_type) ; // convert to upper case

    if( worker_type == 'M' )
    {
        std::cout << "Enter your monthly salary: " ;
        double monthly_salary ;
        std::cin >> monthly_salary ;

        // TO DO: verify that salary is non-negative
        std::cout << "\nYour pay is RM " << monthly_salary << '\n' ;
    }

    else if( worker_type == 'D' )
    {
        std::cout << "Enter total hours worked: " ;
        double hours_worked ;
        std::cin >> hours_worked ;

        // TO DO: verify that hours worked is non-negative
        const double hourly_rate = 25.0 ;
        std::cout << "\nYour pay is RM " << hours_worked * hourly_rate << '\n' ;
    }

    else if( worker_type == 'S' )
    {
        std::cout << "Enter gross computer sales: " ;
        double gross_sales ;
        std::cin >> gross_sales ;

        // TO DO: verify that gross sales is non-negative
        const double base_pay = 500.0 ;
        const double bonus_rate = 0.05 ;
        std::cout << "\nYour pay is RM " << base_pay + gross_sales*bonus_rate << '\n' ;
    }

    else std::cout << "error: unknown worker type\n" ;
}
Last edited on
some languages have an elseif statement, or elif, etc.
c++ does not. c++ has an if statement that may optionally be followed by an else statement.

you can chain those, so that it can even LOOK and behave like an elseif, but be aware they are just chained normal if statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
looks like  this explicitly coded:
if(condition) 
{
  code
}
else 
{
    if (another condition)
      other code
}

which can of course be made to look like this:
if(condition) 
{
  code
}
else if(another condition)  //the if is 'inside' the else statement, without braces due to one liner rule
{
   other code
}


it makes it nice looking, like the other languages, to do it that way.
Last edited on
I managed to do that but now monthly_paid , daily_wages and salesperson have turned into uninitialized variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
	float worker_type, monthly_paid, daily_wages, salesperson, monthly_salary, hours_worked,gross_sales;
	cout << "---Program To Find Pay of a Worker---" << endl;
	cout << "\nEnter Type of Worker:";
	cin >> worker_type;
	if (worker_type = monthly_paid)
	{
		cout << "\nEnter your monthly salary";
		cin >> monthly_salary;
		cout << "\nYour pay is RM" << (monthly_salary);
	}
	else if (worker_type = daily_wages)
	{
		cout << "\nEnter total hours worked";
		cin >> hours_worked;
		cout << "\nYour pay is RM" << (hours_worked * 25);
	}
	else if (worker_type = salesperson)
	{
		cout << "\nEnter gross computer sales";
		cin >> gross_sales;
		cout << "\nYour pay is RM" << (500 + (0.05 * gross_sales));
	}
	else if ()
	{
		cout << "\n Error: unknown worker type";
	}
	return 0;

}
First, one thing that was missed was your use of the = operator.
= is an assignment
== tests for equality. You should be using == in if statements, not =.

Ryan, just do else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (worker_type == monthly_paid)
{
    // ...
}
else if (worker_type == daily_wages)
{
    // ...
}
else if (worker_type == salesperson)
{
    // ...
}
else
{
    cout << "\n Error: unknown worker type";
}


Edit: See JLBorges example, it's much more complete.
Last edited on
Okay I will refer to JLBorges example. JLBorges looks a little too complex for my level but I'll try to understand. Thank you everyone for your help
I mean I just mentioned his because it's a complete example. Mine and jonnin's are fine, too.

In your latest code post, you have (simplified)

1
2
3
4
5
6
7
float worker_type, monthly_paid;
cout << "\nEnter Type of Worker:";
cin >> worker_type;
if (worker_type = monthly_paid)
{
    // ...
}

1. use ==, not = in the if statement.

2. monthly_paid is an uninitialized variable. It doesn't have a valid value. What should it be?

1) when I use == it shows up an error
2) monthly_paid is supposed to be 1 of 3 choices a user can input. If I initialize monthly_paid by making it =0, will it work?
You are asking the user to "enter type of worker". What do you want to happen if the user enters 0? Does a worker type of 0 mean that they are paid monthly?

If so, yes, change your logic to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int worker_type;
cout << "\nEnter Type of Worker:";
cin >> worker_type;
if (worker_type == 0)
{
    // paid monthly
    cout << "logic for being paid monthly goes here" << endl;
}
else if (worker_type == 1)
{
    // paid weekly???
}
// ...
else
{
    cout << "invalid" << endl;
}
Last edited on
Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.
Thanks a lot Ganado it's working fine now :)
For JLBorges example

How did they make the program print out the paragraph in exactly 3 lines without having it all combined into one line?

1
2
3
 std::cout << "\nEnter Type of Worker "
              << "(M for monthly paid, D for daily wages or S for sales person)\n"
              << "M/D/S? " ;
\n is a special marker that means end of line.

@jonin but he used '\n' in his own original post ;p!

@Ryan, Unless inside quotes, the C++ compiler does it read white spaces
So instead of
 
 std::cout << "\nEnter Type of Worker " << "(M for monthly paid, D for daily wages or S for sales person)\n" << "M/D/S? " ;


You can write:
1
2
3
 std::cout << "\nEnter Type of Worker "
              << "(M for monthly paid, D for daily wages or S for sales person)\n"
              << "M/D/S? " ;


Or even

1
2
3
4
5
 std::cout           <<          "\nEnter Type of Worker "
              

<<      "(M for monthly paid, D for daily wages or S for sales person)\n"
              <<                       "M/D/S? " ;


But how is he able to combine multiple statements into one single cout statement?
-> It's called cascading.

The operator << is used for cascading output operations
and >> is likewise used for cascading input operations.

1
2
3
 std::cout << "\nEnter Type of Worker "
              << "(M for monthly paid, D for daily wages or S for sales person)\n"
              << "M/D/S? " ;


is equivalent to typing

1
2
3
 std::cout << "\nEnter Type of Worker ";
          std:: cout << "(M for monthly paid, D for daily wages or S for sales person)\n";
          std::cout << "M/D/S? " ;


The compiler doesn't see any difference in the two examples, so cascading can be useful for increasing readability.

Topic archived. No new replies allowed.