0

All i want is to print something on the screen if the user is pressing the "Enter" key . This is my task: Display a file a line at a time, waiting for the user to press the “Enter” key after each line and this is my fragment of code:

vector<string> v;
ifstream in("in.txt");
string line;
char actiune;

while (getline(in, line)) {
    cin >> actiune;
    if (int(actiune) == int('\n')) {
        cout << line << endl;
    }
    else {
        while (int(actiune) != int('\n')) {
            cin >> actiune;
        }
        cout << line << endl;
    }

}

Trying to solve this i found another question . LEts say we have a variable and we are reading it and after that we want to print its ascii code. Why when pressing enter the code does nothing and waits the user to press another key ?

1 Answers1

1

getline will ignore \n because it reads until \n and then moves to other line

you could use while (in.get(line) /*change variable name...*/). This will read only a char and this way you will be able to check if it is \n or not

CIsForCookies
  • 10,156
  • 5
  • 36
  • 74