Simple If/Else problem

Hello, I am a programming student and I'm glad I found this board. I have a problem I could use some help with.
The program should calculate an employees raise based on which department they work in. When I run the program, it calculates and displays two different raise amounts, one for department A and B(which have the same raise amount), and another for department C. I do not want both amounts to display.

Also, when the program executes, I need to have a data validation step. The program will display the message that states "Please enter A, B, or C" even if the user enters A, B or C. It also continues to execute if the input is not correct.

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
  
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

//declare variables
double salary = 0.00;
double raise = 0.00;
char department = ' ';


//gather input
cout<<"Enter the department. (A, B, or C)"<<endl;
cin>>department;
//validate input
if (department !='A'||'a'||'B'||'b'||'C'||'c')
	cout<<"Please enter A, B, or C."<<endl;
//endif
cout<<"Enter the employee salary."<<endl;
cin>>salary;

//calculate raise
if (department== 'A'||'a'||'B'||'b')
{
	raise = salary *.02;
	cout<<fixed;
	cout<<setprecision(2);
	cout <<"Raise amount is $" <<raise<<endl;
//endif
}
if (department == 'C' || 'c');
{
	raise = salary *.015;
	cout<<fixed;
	cout<<setprecision(2);
	cout<<"Raise amount is $"<<raise;
//endif
}
}
Last edited on
Hey and welcome @JosephA!

You're on the right track, but the if statements needs a bit of a tweak.

if (department== 'A'||'a'||'B'||'b')
You need to explicitly use department ==

It should look like this - if (department== 'A'|| department== 'a'|| department== 'B'|| department== 'b')

Same goes for all the if statements, ofc =)
Last edited on
remove the semi-colon from line 33 as well. That will break your if.
That was huge help!
It no longer displays both calculations.
Thanks to both of you.
What about the data validation? The program still executes with invalid input.
If you want the user to keep re-entering until they're input is equal to A,a,B,b,C or c. You're gonna have to use a loop, a while-loop specifically in this case. See if you can handle it by yourself -

http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm
http://www.cprogramming.com/tutorial/lesson3.html

Awesome, thanks!
Why don't you convert the user input to all capital letter instead of having different conditions, e.g 'a', 'A', 'b' 'B'.
That way, there will be only one option which is capital letter and that will make the conditions look better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}


output:
TEST STRING
Last edited on
Topic archived. No new replies allowed.