0

What am I doing wrong? I want this expression to be displayed/work after my input: cout << "Please enter your Agent ID."; But it would not register the whole word of my input: log in when I type it with space. If it was a word without space it would register it and do the if statement properly if the condition is met.

So I don't know what I am doing wrong.

Here's the Code:

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string input1;
    cout << "Welcome to AgentOS" << endl;
    cout << "To log in, please type \"log in\" " << endl;
    cin >> input1;
    
    if (getline(cin, input1) && input1 == "log in")
    {
        cout << "Please enter your Agent ID." << endl;
    }
    return 0;
}

Instead of display "Please enter your Agent ID", it would just simply return to 0 after I type in log in.

P.S. I am sorry I am still a beginner.

Ali Zain
  • 1
  • 1
  • 1
    Explanation of duplicate: `cin >> input1;` stops reading the first time it encounters a space. It looks like you could just drop that line entirely because you immediately `getline`, which will read an entire line at a time. – Nathan Pierson May 21 '22 at 17:17
  • @NathanPierson AH, oh my Lord! It was so simple, Thank you! – Ali Zain May 21 '22 at 17:18

0 Answers0