C++ doesnt support getline and cin simultaneously?

//C++ code following


#include <iostream>
#include <string>
using namespace std;

int main(){

            string harsh;
            cin>>harsh;


            cout<<harsh;
             string fullName;
cout << "Type your full name: ";
getline(cin,fullName);
cout << "Your name is: " << fullName;


            return 0;

}

Hello!

It works fine to me:

#include <iostream>
#include <string>

using namespace std;

int main() {
    char fullname[256];
    string userName;

    cout << "Type your full name: ";
    cin.getline(fullname, 256);

    cout << "Hello " << fullname << endl;

    cout << "With a string: ";
    getline(cin, userName);

    cout << "Hello " << userName << endl;
    return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

http://www.cplusplus.com/forum/general/51433/

just after reading one word it has to take input from getline(cin,firstName); , But its gets skipped on linuxcodeblocks,

After reading one word, the next thing in the stream is the end-of-line character. That’s what your getline is taking as input. getline input is terminated by the end-of-line character.

1 Like