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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include #include 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)"<>department; //validate input if (department !='A'||'a'||'B'||'b'||'C'||'c') cout<<"Please enter A, B, or C."<>salary; //calculate raise if (department== 'A'||'a'||'B'||'b') { raise = salary *.02; cout<
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.

 ``12345678910111213141516`` ``````/* toupper example */ #include #include 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.