If and Else statements with char data types

What I'm trying to do is use an if and else statements to process a char data type. If one was to input any of the following: S, O, M; then the program would continue on (leaving it to output "null" until I get this figured out). If any other character is typed in, the program would output "Invalid Employee Code".

What I have at the moment however is a program that outputs "null" no matter what I input. I've never used char data types with an if and else statement so I'm at a loss here.

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
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	string fname, lname;
	int idnum, jocode, years, edcode;
	double grsal;
	char emcode;
	bool flag1, flag2;
	
	cout << fixed << showpoint << setprecision(2);

	cout << "Welcome to Badwater Brewery's Gross Salary Calculator" << endl
		<< "Input your name [John Smith]:" << endl;
	cin >> fname >> lname;
	cout << "Input your Employee Code [X]:" << endl;
	cin >> emcode;
	cout << "Input your Job Classification Code [x]:" << endl;
	cin >> jocode;
	cout << "Input your Years of Service [xx]:" << endl;
	cin >> years;
	cout << "Input your Educational Code [x]:" << endl;
	cin >> edcode;

	flag1 = false;
	if (emcode == 'S'||'O'||'M')
	{
		flag1 = true;
		cout << "null" << endl;
	}
	else
	{
		cout << "Invalid Employee Code" << endl;
	}
}
Hi,

This line,

if (emcode == 'S'||'O'||'M')

should be:

if (emcode == 'S'|| emcode == 'O' || emcode == 'M')

Some other tips:

Always declare and initialise your variables, one per line. That way you know you have initialised everything, and not missed one sometime later in your code. One per line is good, so you can comment things like expected ranges of values, or any other useful info.

Don't be afraid to have longer variable names, if it make them more meaningful and aids understanding - especially of others are going to read your code. For example, I would have had EmployeeCode and EducationCode. This is an advantage when you start to have larger projects.

EDIT:

Don't have using namespace std; - google to see why this is so, and what to do instead.

Hope all is well :+)
Last edited on
Thanks a ton mate. Going to take your advice on the variable names.

Can you explain what my line meant in plain English before hand? It would help to know what exactly I was telling the compiler what to do there.
Hi,

The way the compiler parses your statement:

First, it evaluates emcode == 'S' which could be true or false. Then there is the OR operator, but there is no conditional associated with the right hand side, so it evaluates to true. This makes the result of the OR statement to always being true. The same follows for the other OR statements. So line 30 will always be true.

If you are using an operator, function, class or algorithm for the first time: then read the documentation for it, rather than rely on what you are thinking as to how it works.

Be careful using std::cin - it only reads up to the first whitespace. For example if someone has a surname of "Di Masi" , then you won't get the expected results. Use std::getline instead.

Did you read up about namepsaces? Ideally one should put their code in it's own namespace.

In C++, you can leave the declaration of variables until just before you need to use it. So flag1 could be declared just before line 29.

Investigate splitting your code into functions.
Topic archived. No new replies allowed.