c++ - Can someone tell me why I am stuck in my validation loop after entering 'y' to continue? -


why getting stuck in validation loop after hitting y continue? have use cin.get , can not use strings this program collects input user , displays them using pointer array, have validate negative numbers, letters , newline characters appropriate message

#include <iostream> #include <iomanip> using namespace std;  void validateuserinput(char *userinputchararray, int &strlength);  int const array_size = 100;  int main() {      char *userinputchararray = nullptr;     char yes = null;     int lengthofinput;         //creating dynamic array size 100     userinputchararray = new char[array_size];      //loop         {         int count = 0;          //user prompt , accept input          cout << "please enter integer >= 0 , press <enter>: " << endl;         cin.get(userinputchararray, array_size);           while(userinputchararray[count] != ' ' && userinputchararray[count]     != '\0')         count++;         {         if(userinputchararray[count] == ' ')             {                 userinputchararray[count] = '\0';             }         }         lengthofinput = count;          validateuserinput(userinputchararray, lengthofinput);          cout << "your number is: " << endl;         for(int = 0; < lengthofinput; i++)         {             cout << userinputchararray[i] - '0' << " ";         }         cout << endl;          cout << "press y continue or other button exit: " <<endl;         cin >> yes;          delete [] userinputchararray;         userinputchararray = nullptr;         userinputchararray = new char[array_size];      }while(yes == 'y' || yes == 'y');       cout << "thank good-bye";     cout << endl;      delete [] userinputchararray;     userinputchararray = nullptr;      system("pause");     return 0; } 

im getting stuck in functions while loop

void validateuserinput(char *userinputchararray, int &strlength) {     int counter = 0;      while(*userinputchararray < '0' || (*userinputchararray >= 'a' &&    *userinputchararray <= 'z')          || (*userinputchararray >= 'a' && *userinputchararray <= 'z')          || *userinputchararray == 0)     {         cout << "please enter positive integer , press <enter>: "    <<endl;         cin.get(userinputchararray, array_size);     }      while(userinputchararray[counter] != ' ' && userinputchararray[counter]   != '\0')         counter++;      if(userinputchararray[counter] == ' ')     {         userinputchararray[counter] = '\0';     }     strlength = counter; } 

according while loop have here, keep on calling in.get long first character read in digit or alphabetic character. so, if start input 1 of characters, loop forever.

i'd suggest input line @ time , parse get.


Comments