#include <iostream>
#include <string>

using namespace std;
int main()

{
    string x;
    cout<<"Enter Text : "<<endl;
    cin>>x;
    getline(cin,x);
    cout<<"The text entered was, ";
    cout<<x;

    system("PAUSE");
    return 0;
    }

Output that I get when I enter 1 word :

Enter Text :
LOL
The text entered was,
Press any key to continue . . .

Output I get when I enter 2 words :

Enter Text :
LOL LMFAO
The text entered was, LMFAO
Press any key to continue . . .

This is the issue, any help will be greatly appretiated. Thanks !

Recommended Answers

All 4 Replies

cin>>x reads the first word in the stream. getline(cin,x) reads the rest of the line. If there's nothing else on the line, getline won't read anything.

It seems like you're expecting these two operations to not remove what they read from the stream, which isn't the case.

cin >> x already grabs the input you provide. The second call (getline(cin, x)) attempts to do the same thing.

That is why when you provide a second input you get output of the second token.

eads the first word in the

eads the first word in the

reads the rest of the line. If th

reads the rest of the line. If th

ord in the

. If there's nothing

. If there's nothing

ems like you're expecting these two operations to not remove what they read f

Hey! So I need to read the whole line not just the first word or the one that follows it. How will I do this?

Get rid of line 10 and it should work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.