vendredi 25 septembre 2015

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

#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);


        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;
}
void validateUserInput(char *userInputCharArray, int &strLength)
{
    int counter = 0;

why am i getting stuck here? while(*userInputCharArray < '0' || (*userInputCharArray >= 'A' && *userInputCharArray <= 'Z') || (*userInputCharArray >= 'a' && *userInputCharArray <= 'z') || *userInputCharArray == 0) { cout << "Please enter a positive integer and press : " <

    while(userInputCharArray[counter] != ' ' && userInputCharArray[counter]   != '\0')
        counter++;

    if(userInputCharArray[counter] == ' ')
    {
        userInputCharArray[counter] = '\0';
    }
    strLength = counter;
}

Aucun commentaire:

Enregistrer un commentaire