Search code examples
c++cinentergetch

How do I detect hitting enter twice to let user exit from populating array with for loop?


I have to let user fill an array with 0-500 values, and at any moment they can exit entering the array with two consecutive empty value inputs (hitting enter twice).

Then the array displays, then it sorts itself, and finally displays again.

I have no idea how to capture enter twice in a row to exit the loop. The best I can do to help communicate what I am doing, I used getch(); which I am pretty sure my teacher would not like since it isn't standard.

I've tried cin.peek and cin.get but if there is nothing in the stream it always waits for enter which I don't want happening.

I found getch(); and it basically bypasses the problem I am having. I need to make this program operate the same way but using something that is in the c++ standard. Hitting enter after every entry is fine, but I need to detect an empty line and then go into a state where I can detect another one. If it detects one empty line I need to be able to resume entering values.

Here is a somewhat working version with getch(); of what I am going for.


#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

const int ARRAY = 500;

int GetUserArray(int b[ARRAY]);
void DisplayVals(int ArrayofValues[ARRAY], int &x);
void SortArray(int ArrayofValues[ARRAY], int& x);

int main()
{
    int UserArray[ARRAY]= {0};
    int NumberofValues = 0;

    //User Populates Array, catches number of entries with return
    NumberofValues = GetUserArray(UserArray);

    //Displays the NON-SORTED array
    DisplayVals(UserArray, NumberofValues);

    //Sorts Array
    SortArray(UserArray, NumberofValues);

    //Displays the SORTED array
    DisplayVals(UserArray, NumberofValues);

    return 0;
}

int GetUserArray(int UserArray[ARRAY])
{
    int howmany = 0;
    int input = 0;

    //ASCII code for enter key is 13 '\n'
    int x = 0;

    cout << "Please enter an integer values: ";

    for (int i = 0; i < ARRAY; i++)
    {

        input = _getche();


        if (input == 13)
        {
            cout << endl;
            cout << "Please enter an integer value: ";

            input = _getche();
            if (input == 13)
            {
                i += 499;
                cout << endl;
                cout << endl;
                cout << "Exiting...";
            }

        }
        UserArray[i] = input;
        howmany++;
    }
    return howmany;
}



void DisplayVals(int ArrayofValues[ARRAY], int &x)
{
    cout << '\n' << endl;

    for (int i = 0; i < x ; i++)
    {
        char Display = 0;
        Display = ArrayofValues[i];
        cout << Display << ' ';

    }

    cout << endl;

}

void SortArray(int ArrayofValues[ARRAY], int& x)
{
    bool sorted = false;

    for (int pass = 0; pass < x; pass++)
    {
        for (int i = 1; i < (x - pass - 1); i++)
        {
            if ((ArrayofValues[i - 1]) < ArrayofValues[i])
            {
                int temp = ArrayofValues[i - 1];
                ArrayofValues[i - 1] = ArrayofValues[i];
                ArrayofValues[i] = temp;

            }
        }

    }



}

Solution

  • Note: ASCII value of \n is 10.

    Following is the tested and working code:

    int GetUserArray(int UserArray[ARRAY]) {
        int input = 0;
        int exit_flag = 0;
        int howmany = 0;
    
        cout << "Please enter an integer values: ";
        for (int i = 0; i < ARRAY; i++) {
            input = cin.get(); //Stores ASCII value inside input
            if(input==10){
                if(exit_flag){
                    i+=ARRAY; // You should not hard-code any values.
                    cout<<"Exiting";
                }
                exit_flag = 1;
                --i; //If you don't want to skip an index in case of Single Enter.
                continue;
            }
            else{
                exit_flag=0;
                UserArray[i] = input-'0'; //Converting ASCII to INT.
                howmany++;
                cin.ignore(); //Clearing the input buffer as we have pressed 'Enter'.
            }
        }
        return howmany;
    }
    

    Thank you.