bool isDigital(char c) { return ('0' <= c && c <= '9'); }
void DigitalToken( char digitToken[50], char ch ) {
digitToken[0] = ch;
char input = '\0';
cin >> input;
int i = 0;
while ( ( input != ' ' ) && ( input != '\t' ) && ( input != '\n' ) ) { // got a infinite loop
i++;
digitToken[i] = input;
cin >> input;
} // while ( input != ' ' && input != '\t' && input != '\n' )
} // DigitalToken()
int main() {
char ch = '\0';
while ( cin >> ch ) {
if ( isDigital(ch) ) {
char* digitToken = new char[50]();
DigitalToken(digitToken, ch);
cout << digitToken;
delete[] digitToken;
} // else if
} // while
} // main()
我不明白为什么在 DitgitalToken
函数中出现无限循环。
当我输入123
时,应该输出123
。
我看了很久,但还是不知道为什么以及如何解决。
最佳答案
而不是 cin >> input;
使用input = cin.get();
.
关于c++ - while 函数中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66916353/