+ 2
Code is running but only giving output You will work in rural area even if I input different ages
#include <iostream> using namespace std; int main() { int age ; cin >> age ; char gender ; char M , F ; if (gender==F) { cout << "You will work in rural area" ; } else if (gender==M && age>=20 && age<40) { cout << "You will work in urban area" ; } else if (gender==M && age>=40 && age<60) { cout << "You will work anywhere" ; } else { cout << "Error 404" ; } return 0; }
8 Antworten
+ 2
gender variable must be initialized
M and F are null character variables instead of that use 'F' and 'M' directly in the if-else section
+ 2
Gender variable must be initialised , plz explain I didn't understand
+ 2
Keshav Karn Sakshi have given the correct code but before copying it you must understand it
+ 1
Keshav Karn take input in gender variable
cin>>gender
+ 1
include <iostream>
using namespace std;
int main() {
int age ;
cin >> age ;
char gender
cin >> gender ;
if (gender==F) {
cout << "You can work in school" ;
}
else if (gender==M && age>=20 && age<40){
cout << "You can work in Urban area" ;
}
else if (gender==M && age>=40 && age<60)
{
cout << "You cannot work" ;
}
else {
cout << "Error 404" ;
}
return 0;
}
+ 1
Silly mistake , char gender ;
But instead code not running
How to define M and F ?
+ 1
Now your code works:-
#include <iostream>
using namespace std;
int main() {
int age ;
cin >> age ;
char gender;
cin >> gender ;
if (gender=='F') {
cout << "You can work in school" ;
}
else if (gender=='M' && age>=20 && age<40){
cout << "You can work in Urban area" ;
}
else if (gender=='M' && age>=40 && age<60)
{
cout << "You cannot work" ;
}
else {
cout << "Error 404" ;
}
return 0;
}
+ 1
I understand sir , there is only one mistake