-
Error ?
I am making this program but It doesnt works properly if different values are entered
Write program which will take person’s gender, material status(in characters) and age as input and will tell that the person is eligible to get job in Departement of Defense or not.
Creiteria:
FOR MALE:
Age should be between 26-39
He can be married or single
Gender should be male
FOR FEMALE
Age should be between 20-28
She should be single
Gender should be female
INPUT:
ENTER MATERIAL STATUS:S
ENTER AGE: 28
ENTER GENDER: M
OUTPUT:
CRITERIA MET
NOTE:
1. if user enter a character for material status other of S or M and for gender other than M OR F program should give a error and teriminate.
Code:
void main()
{
char g,ms;
int age;
clrscr();
if(g=='M' && g!='F' && ms!='M' || ms!='S')
{
cout<<"\n Gender : ";
cin>>g;
cout<<"\n Age : ";
cin>>age;
cout<<"\n Marital status : ";
cin>>ms;
if(g=='M' && age>=26 && age<=39)
cout<<"\n Criteria Met ";
else if(g=='F' && age>=20 && age<=28 && ms=='S')
cout<<"\n Criteria Met ";
else
cout<<"\n Invalid Info ";
}
else if(g!='M' || g!='F' && ms!='M' || ms!='S')
cout<<"\Error";
exit(0);
}
getch();
-
Code:
if(g=='M' && g!='F' && ms!='M' || ms!='S')
This test is completely meaningless since the variables g and ms are unintialized at that point - they could contain any random value.
-
anon can you please correct it , I have tried this program from different steps still cant make it write :(
-
Quote:
ENTER MATERIAL STATUS
Are you sure this isn't supposed to be MARITAL STATUS?
http://en.wikipedia.org/wiki/Marital_status
anon is right, where your if-statement sits, the variables are uninitialized, so could have any values.
--
Mats
-
hehe i just copied the question sorry its marital status :D
check this code , i just want to know how to do "if user enter a character for material status other then S or M and for gender other than M OR F program should give an error and teriminate."
Code:
void main()
{
int age;
char g,m;
clrscr();
cout<<"\n Gender : ";
cin>>g;
cout<<"\n Age : ";
cin>>age;
cout<<"\n Marital Status : ";
cin>>m;
if(g=='M'&& age>=26 && age<=39 && m=='S' || m=='M')
cout<<"\n criteria met ";
else if(g=='F' && age>=20 && age<=28 && m=='S'&& m!='M')
cout<<"\n Criteria met ";
else
cout<<"\n ERROR!! ";
exit(0);
getch();
}
-
Code:
if(g=='M'&& age>=26 && age<=39 && m=='S' || m=='M')
else if(g=='F' && age>=20 && age<=28 && m=='S'&& m!='M')
Some slight modification to those tests are needed:
Code:
if(g=='M'&& age>=26 && age<=39 && (m=='S' || m=='M'))
else if(g=='F' && age>=20 && age<=28 && m=='S')
Also, main should return int.
[edit]
And the getch is pointless since the exit is placed before it meaning that the code will never be reached... not to mention that a call or two to std::cin.get() would work just as well as a nonstandard getch call.
[/edit]
-
You need to specify priority as hk_mp5kpdw kindly points out, because
if (g == 'M' && age >= 26 && age <= 39 && m == 'S' || m == 'M')
Can mean:
- If gender is male and age is 26 or more, but 39 or below, and either single or married.
if (g == 'M' && age >= 26 && age <= 39 && (m == 'S' || m == 'M'))
- If gender is male and age is 26 or more, but 39 or below and single
OR
- If the individual in mind is simply married.
Which is not what you want and this formula looks like:
if ((g == 'M' && age >= 26 && age <= 39 && m == 'S') || m == 'M')
So when you combine and and or, use () to show what you actually mean! Even if it makes sense to you and the compiler and you KNOW it is right, others will most likely not and might either be confused or think you have made an error.