简体   繁体   English

有人可以告诉我为什么输入“ 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? 为什么在点击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.

相关问题 有人可以告诉我为什么即使参数正确也出现此错误? - Can someone tell me why am i getting this error even though the arguments are correct? 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人能告诉我为什么吗? - I am trying to find the height of a Binary tree with this code, but it keeps returning 0, can someone tell me why? 有人可以告诉我为什么传递参数后我的字符串为空吗? - Can someone tell me why my string is blank after passing the argument? 有人可以告诉我为什么我的分数总是计算为零? - Can someone tell me why my fraction always calculates as zero? 有人能告诉我为什么会收到这个错误吗? - can someone tell why am i getting this error? 有人能告诉我为什么这不是一个持续的表达? - Can someone tell me why this is not a constant expression? 请问有人请告诉我为什么我的for()循环不会一直计算在内? - Could someone please just tell me why my for() loop will not count all the way up? 我的复制构造函数导致使用我的类的方法失败。 有人可以看一下,然后告诉我我做错了什么吗? - My copy constructor is causing my methods which use my class to fail. Can someone take a look at it and tell me what I am doing incorrectly? 有人可以告诉我为什么会收到此错误消息吗? 内联 constexpr 变量 - Can someone tell me why I get this error message? inline constexpr variable 有人可以告诉我我的代码有什么问题吗? - Can someone please tell me what is wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM