Help with loop printing too many times [Solved but wondering if there's a more elegant solution]
The task is: /* Write a program that takes in a string representing a zip code. Output true or false if it is a valid zip code or not. A valid zip code is only numbers, must be 5 characters in length, and contain no spaces. Input Format: A string containing a zip code. Output Format: A string: true is the input is a valid zip code, or false, if it is not. Sample Input: 752f78 Sample Output: false */ Code works but "true" prints too many times : #include <cctype> #include <iostream> using namespace std; int main() { string str; cin >> str; int check; int strlen = str.length(); for (int i = 0; i < strlen; ++i) { check = isalpha(str[i]); if ((check > 0) || (str.length() > 5)){ cout << "false"; break; } else cout << "true"; } } return 0; } Any suggestions? Should I be using a different type of loop like while or do... while? Boolean? https://code.sololearn.com/c843d1ksTwqx/?ref=app