Angela
Angela

Reputation: 15

C++: Accepting user input until *two* blank lines are received

I am writing code concerned with the user's input. I would like to keep on receiving the user's input until the user enters two blank lines (the user consecutively hits the 'return' button twice for the two blank lines and on the third 'return' the user input terminates.)

The code I have so far is:

int number_of_empty_res = 0;
string data; 

vector<string> myvec;

while (getline(cin, data) && !data.empty())
{
    
    if (data == "")
    {
        number_of_empty_res += 1;
        myvec.push_back (data+' ');

    }
  
    if (number_of_empty_res == 2)
    {

        break;
    }
    else
    {
        number_of_empty_res = 0;
        myvec.push_back (data+' ');
        
    }
    
}


for (int i = 0; i < myvec.size(); i++)
{
    
        cout << myvec[i];
    
}

I am using a vector to act as a buffer,to push back each line of input into the vector, as I would like to do some useful stuff with the users input. I am also keeping count of the number of consecutive blank lines.

Currently, the code I have terminates after the user consecutively hits the 'return' button twice (ie. the user gives me one blank line, and when the 'return' key is pressed again the program ends). I was working through my code, and I thought that if I have the 'if' condition of there being two consecutive blank lines, the code will break, but this is not happening... I have tried increasing the number of blanks specified, but it seems this actually has no effect on the code, as the code keeps terminating as described previously. I have also experimented with the condition of the 'while' loop. I thought that maybe the '!data.empty()' may have been the problematic part of my code, but upon removing it, the user input never seems to terminate.

I appreciate any help and ideas! Thank you!

Upvotes: 1

Views: 163

Answers (1)

user15401571
user15401571

Reputation:

Try this:

int main() {
    int number_of_empty_res = 0;
    string data;

    vector<string> myvec;

    while (getline(cin, data))
    {
        if (data.empty()) {
            number_of_empty_res += 1;
            if (number_of_empty_res > 2)
            {
                break;
            }
        }
        else 
        {
            number_of_empty_res = 0;
            myvec.push_back(data + ' ');
        }

    }


    for (int i = 0; i < myvec.size(); i++)
    {

        cout << myvec[i];

    }
}

The cause: If we look inside your loop, we see this:

    if (data == "")
    {
        number_of_empty_res += 1;
        myvec.push_back (data+' ');

    }
  
    if (number_of_empty_res == 2)
    {

        break;
    }
    else
    {
        number_of_empty_res = 0;
        myvec.push_back (data+' ');
        
    }

The problem here is, if number_of_empty_res is NOT yet 2, the test fails, and therefore we reset number_of_empty_res accidentally.

Note: It should be a good habit to use a debugger, if you don't already.

Upvotes: 1

Related Questions