0
#include <iostream>

using namespace std;

string input() {
    string str;
    char ch;

    cout << "Enter string: ";
    getline(cin, str);
    cout << "\n" << str;

    cout << "\nChange the string? ";
    cin >> ch;

    if (tolower(ch) == 'y')
        input();

    return str;
}

int main() {
    string str;
    str = input();

    //some other code
}

When trying to change the string, the getline() function is reading some whitespace or blank line. I am not getting how to resolve this issue. I know that getline() is used to read the entire line (including white spaces).

The first time it is working fine, but for the next recursive call, getline() is reading something blank. How to make getline() wait for user input?

0 Answers0