简体   繁体   中英

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]     != '\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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM