[英]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?
为什么在点击Y继续后会陷入验证循环? 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
我必须使用cin.get且不能使用字符串此程序从用户处收集输入并通过使用带有数组的指针来显示它们,我必须使用适当的消息来验证负数,字母和换行符
#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] != '\0')
count++;
{
if(userInputCharArray[count] == ' ')
{
userInputCharArray[count] = '\0';
}
}
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] != '\0')
counter++;
if(userInputCharArray[counter] == ' ')
{
userInputCharArray[counter] = '\0';
}
strLength = counter;
}
According to the while
loop you have here, you will keep on calling in.get
so long as the first character that you read in is a digit or an alphabetic character. 根据此处的
while
循环,只要您读入的第一个字符是数字或字母字符,就将继续调用in.get
。 So, if you start your input with one of those characters, you will loop forever. 因此,如果以这些字符之一开始输入,则将永远循环。
I'd suggest that you get input a line at a time and then parse what you get. 我建议您一次输入一行,然后解析得到的内容。
cin.get(*userInputCharArray);
将仅提取一个字符,因此终止符'\\ 0'将不属于userInputCharArray
。
Change it to read a line at a time and parse the input. 将其更改为一次读取一行并解析输入。 If you're still having issues, you can flush your input buffer before each read like so:
如果仍然有问题,可以在每次读取之前刷新输入缓冲区,如下所示:
cin.ignore(10, "\n");
cin.get(userInputCharArray, ARRAY_SIZE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.