Tseronaught Tseronaught - 1 month ago 26
C++ Question

If statement skipping else if straight to else

I'm attempting to make the statement below do the following.
If age is greater than 20 and less than 50, continue.
If age is less than 20 and greater than 50, error restart.
if anything else, error, restart.

But, for some reason it skips right over the else if condition and goes straight to else. If I input "19" for age, it outputs "Error.", if I input 51 for age, it outputs "Error." Whats up?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int plyAgeCreate() {

int plyAge = 0;

cout << "Enter an age" << endl;
cin >> plyAge;

//If age is greator than 20 and less than 50, accept.
//Else if age is less than 20 but greator than 50, decline.
//else, error. Restart.

if (plyAge >= 20 && plyAge <= 50) {
cout << "Welcome!" << endl;
}
else if (plyAge < 20 && plyAge > 50) { //Why is this being skipped?
cout << "Between 20 and 50" << endl;
return plyAgeCreate();
}
else {
cout << "Error" << endl;
return plyAgeCreate();
}



}


int main()
{

plyAgeCreate();

system("pause");
return 0;
}

Answer

use or, not and. it will only go in there if the number somehow is less than 20 and more than 50, which is impossible. if it was 19 it would have to change to 51, either change it to this or delete it.

else if (plyAge < 20 || plyAge > 50) { //Why is this being skipped?