+ 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; }

16th Nov 2022, 3:12 PM
Keshav Karn
Keshav Karn - avatar
8 Answers
+ 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
16th Nov 2022, 3:13 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 2
Gender variable must be initialised , plz explain I didn't understand
16th Nov 2022, 3:51 PM
Keshav Karn
Keshav Karn - avatar
+ 2
Keshav Karn Sakshi have given the correct code but before copying it you must understand it
16th Nov 2022, 5:05 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 1
Keshav Karn take input in gender variable cin>>gender
16th Nov 2022, 3:54 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 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; }
16th Nov 2022, 4:41 PM
Keshav Karn
Keshav Karn - avatar
+ 1
Silly mistake , char gender ; But instead code not running How to define M and F ?
16th Nov 2022, 4:41 PM
Keshav Karn
Keshav Karn - avatar
+ 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; }
16th Nov 2022, 5:01 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 1
I understand sir , there is only one mistake
17th Nov 2022, 2:00 AM
Keshav Karn
Keshav Karn - avatar