OuTPuT PRoBLeM

I am at basic learning level. i got a task of programming
i have done it but the output is not well
The program is supposed to print salary is 275 if we input j
but it is continuously displaying salary is 400 even if we enter f as input

[/code]#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char status;
cout<<"Enter your status \n Enter j for junior or S for senior";
cin>>status;
if(status=='s'|'S')
{
cout<<"Salary is 400";
}
else if(status=='j'|'J')
{
cout<<"Salary is 275";
}
else
{
cout<<"Error";
}
getch();
}
Please use code tags for your all your code - http://www.cplusplus.com/articles/jEywvCM9/

The OR operator is || and not |.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char status;
	cout << "Enter your status \n Enter j for junior or S for senior";
	cin >> status;
	if (status == 's' || status == 'S')
	{
		cout << "Salary is 400";
	}
	else if (status == 'j' || status ==  'J')
	{
		cout << "Salary is 275";
	}
	else
	{
		cout << "Error";
	}


#include<iostream.h> should be #include <iostream>

void main() should be int main()

Chances are whatever source or website taught you to write this program, that it wasn't a very good one.

Edit:

#include<conio.h> is also not part of the c++ standard. Why do you have it there?
Last edited on
I am using turbo c++ for my program that supports the syntax in that way
i.e. [/code] #include<iostream.h> and [/code] void main()
using [/code] #include<conio.h> for [/code] getch();


Used [/code] || But its not working
Last edited on
I gave you the link to an article which explains how to use code tags proparly. But regardless, it's under the format section <>.


Used || But its not working

That's because changing to || is not enough. Did you look at the code I gave you? It should work perfectly fine with it.

if (status == 's' || status == 'S') // Notice status == on both side of the fence
Last edited on
OP wrote:
I am using turbo c++ for my program that supports the syntax in that way


It's not your syntax that is the problem, it is your logic. Look at what TarikNeaj posted:
if (status == 's' || status == 'S')
Then compare it to what you posted for the same input evaluation:
if(status=='s'|'S')
What you are telling the program to do is to apply a bitwise OR operation to the characters 's' and 'S' and then determine if that is equal to what the user input. Whereas TarikNeaj is telling the program to evaluate whether either of the conditions are true.
Topic archived. No new replies allowed.