有人能告诉我为什么在输入 'y' 继续后我会卡在验证循环中吗?
Can someone tell me why I am stuck in my validation loop after entering 'y' to continue?
Why am I getting stuck in my validation loop after hitting Y to continue? I have to use cin.get and can not use strings
This program collects input from a user and displays them by using a pointer with an array, I have to validate for negative numbers, letters and newline characters with the 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 a dynamic array size 100
userInputCharArray = new char[ARRAY_SIZE];
//loop
do
{
int count = 0;
//user prompt and accept input
cout << "Please enter an integer >= 0 and press <ENTER>: " << endl;
cin.get(userInputCharArray, ARRAY_SIZE);
while(userInputCharArray[count] != ' ' && userInputCharArray[count] != '[=11=]')
count++;
{
if(userInputCharArray[count] == ' ')
{
userInputCharArray[count] = '[=11=]';
}
}
lengthOfInput = count;
validateUserInput(userInputCharArray, lengthOfInput);
cout << "Your number is: " << endl;
for(int i = 0; i < lengthOfInput; i++)
{
cout << userInputCharArray[i] - '0' << " ";
}
cout << endl;
cout << "Press y to continue or any other button to exit: " <<endl;
cin >> yes;
delete [] userInputCharArray;
userInputCharArray = nullptr;
userInputCharArray = new char[ARRAY_SIZE];
}while(yes == 'y' || yes == 'Y');
cout << "Thank you Good-Bye";
cout << endl;
delete [] userInputCharArray;
userInputCharArray = nullptr;
system("pause");
return 0;
}
Im getting stuck in my 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 a positive integer and press <ENTER>: " <<endl;
cin.get(userInputCharArray, ARRAY_SIZE);
}
while(userInputCharArray[counter] != ' ' && userInputCharArray[counter] != '[=12=]')
counter++;
if(userInputCharArray[counter] == ' ')
{
userInputCharArray[counter] = '[=12=]';
}
strLength = counter;
}
根据此处的 while
循环,只要您读入的第一个字符是数字或字母字符,您就会继续调用 in.get
。因此,如果您以这些字符之一开始输入,您将永远循环。
我建议您一次输入一行,然后解析您得到的内容。
cin.get(*userInputCharArray);
将只提取一个字符,因此终止字符'\0'不会成为userInputCharArray
的一部分。
将其更改为一次读取一行并解析输入。如果您仍然遇到问题,您可以在每次读取之前刷新输入缓冲区,如下所示:
cin.ignore(10, "\n");
cin.get(userInputCharArray, ARRAY_SIZE);
Why am I getting stuck in my validation loop after hitting Y to continue? I have to use cin.get and can not use strings This program collects input from a user and displays them by using a pointer with an array, I have to validate for negative numbers, letters and newline characters with the 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 a dynamic array size 100
userInputCharArray = new char[ARRAY_SIZE];
//loop
do
{
int count = 0;
//user prompt and accept input
cout << "Please enter an integer >= 0 and press <ENTER>: " << endl;
cin.get(userInputCharArray, ARRAY_SIZE);
while(userInputCharArray[count] != ' ' && userInputCharArray[count] != '[=11=]')
count++;
{
if(userInputCharArray[count] == ' ')
{
userInputCharArray[count] = '[=11=]';
}
}
lengthOfInput = count;
validateUserInput(userInputCharArray, lengthOfInput);
cout << "Your number is: " << endl;
for(int i = 0; i < lengthOfInput; i++)
{
cout << userInputCharArray[i] - '0' << " ";
}
cout << endl;
cout << "Press y to continue or any other button to exit: " <<endl;
cin >> yes;
delete [] userInputCharArray;
userInputCharArray = nullptr;
userInputCharArray = new char[ARRAY_SIZE];
}while(yes == 'y' || yes == 'Y');
cout << "Thank you Good-Bye";
cout << endl;
delete [] userInputCharArray;
userInputCharArray = nullptr;
system("pause");
return 0;
}
Im getting stuck in my 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 a positive integer and press <ENTER>: " <<endl;
cin.get(userInputCharArray, ARRAY_SIZE);
}
while(userInputCharArray[counter] != ' ' && userInputCharArray[counter] != '[=12=]')
counter++;
if(userInputCharArray[counter] == ' ')
{
userInputCharArray[counter] = '[=12=]';
}
strLength = counter;
}
根据此处的 while
循环,只要您读入的第一个字符是数字或字母字符,您就会继续调用 in.get
。因此,如果您以这些字符之一开始输入,您将永远循环。
我建议您一次输入一行,然后解析您得到的内容。
cin.get(*userInputCharArray);
将只提取一个字符,因此终止字符'\0'不会成为userInputCharArray
的一部分。
将其更改为一次读取一行并解析输入。如果您仍然遇到问题,您可以在每次读取之前刷新输入缓冲区,如下所示:
cin.ignore(10, "\n");
cin.get(userInputCharArray, ARRAY_SIZE);