Which command is available in C++ for taking multiple words as an input like gets(is available in C language)??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Which command is available in C++ for taking multiple words as an input like gets(is available in C language)???

C++ Beginner

17th Jul 2018, 7:11 AM
Jaydeep Machhi
Jaydeep Machhi - avatar
4 Answers
+ 6
Taking multiple words as part of a longer string could be achieved by using getline() function. Unlike cin stream object which throws away anything after encountering the first space on the inputted sequence, getline() would keep the whole thing including the spaces between each character group (word). ●●usage●● string userInput = ""; cout << "Your input is "; cin >> userInput; Input is : "C++ Programming" and the stored sequence in userInput variable is "C++". getline(cin, userInput); Now, with the same input as before, the stored sequence in userInput variable is "C++ Programming"
17th Jul 2018, 8:45 AM
Babak
Babak - avatar
+ 5
getline command syntax getline(cin,varname) string h, getline(cin,h);
17th Jul 2018, 10:32 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
Hi Jaydeep Machhi can you give an example of what input were you expecting, and how you expect the output to be? maybe explain in question Description to be more specific. Thanks : )
17th Jul 2018, 8:30 AM
Ipang
+ 1
like if I want to take input as a 'Solo learn' now I can easily take it as an input in C language by simply using 'gets' but I can't use 'gets' in C++.
17th Jul 2018, 8:33 AM
Jaydeep Machhi
Jaydeep Machhi - avatar