Right now I am trying to limit the input of the string for this zip code evalulator. but I think I am doing string str (5); this part wrong. can some explain what is wrong here?
int main ()
{
string str;
cout << "please enter a zip code" << endl;
cin >> str;
if (str.size() <= 5){
cout << "valid zip code" << endl;
}else {
cout << "non valid zip code" << endl;
}
return 0;
}
You're having the user input set to one int, instead of the entire zipcode,
the easiest way is to have them enter in the zipcode as a string, then check to see if its size is greater than 5.
You don't need to include any string length restriction on the input of 'str' because you evaluate 'str' later in the code for its validity anyway - this is what Scorpic and Raezzor are saying. Its unnecessary to have both methods of validation (i.e. your own 'IF' statement and string input length restriction)