this is a function that checks if a person is a man or a woman by checking the second
last element of his social security number. If the number is even then the person is a
woman. If odd then a men. The code is working in a strange way. Sometimes it does the job
and sometimes not. this is the code:
char check_gender(string person_nummer){
int check_digit = (person_nummer.back() - 1) - '0';
char gender;
if(check_digit % 2 == 0){
gender = 'K'; // K for a women(kvinna in swedish)
}
else{
gender = 'M'; // M for man
}
return gender;
}
int main(){
string number;
cout << "enter number" << endl;
cin >> number;
cout << check_gender(number) << endl;
return 0;
}
input1: 8602024898
output1: M // correct output
input2: 8510309159
output1: K // wrong output
input3: 7102022980
output M // wrong output
input4: 4906147410
output M // correct output
weird!