c++ - Why is this while loop not behaving as I expect? -


the problem while loop isn't exiting if user has entered correct name format if first input entered incorrectly. problem doesn't occur when user enters name correctly first time.

void inputclass::validatename(string name) {     bool flag = true     while (flag == true) {          (int i=0;  < name.length(); i++) {              if (name[i] != ' ') {                  if (!isalpha(name[i])) {                      cout << "you have entered incorrectly. please try again. "  << endl;                      setname();                  } else if (i == name.length()-1) {                      cout << "welcome " << name << endl;                     flag = false;   //break out of while loop                  }             }         }     } }  void inputclass::setname() {      string name;     cout << "please enter name: " << endl;     getline(cin, name);     validatename(name); } 

you seem have difficulties finding logic solving problem. start pseudo-code , start implementing once know need. guess looking like

bool valid = false while not valid:     name = setname()     valid = validate(name) 

then there's no need in recursion. implement setname() reads user input , returns string. , validate() performs validation , returns bool. simple, huh?


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -