0

How to get input in the code playground?

For example: cout<<"please enter your name"<<endl; char s[100]; cin.get(s,100); --------------------------------------------------- This is the first time to write a code using the playground, and when I run the code the compiler don't let me enter the name...so how can I solve this?

24th Jan 2019, 9:55 AM
Ahmad Aziz
Ahmad Aziz - avatar
6 Answers
+ 8
Right! it only reads the stream until it finds a whitespace, for this purpose then, I suggest you to use string instead of char array for input buffer, here's a little example: #include <string> // <- need this for string #include <iostream> using namespace std; int main() { cout << "Please enter your name" << endl; string s; // use std::string instead of char array getline(cin, s); cout << "Hello " << s << endl; return 0; }
24th Jan 2019, 10:35 AM
Ipang
+ 7
Hi Ahmad Aziz, welcome to SoloLearn ... : ) It seems the Code Playground does not really get along with cin.get(), you only get to use it in your PC or laptop. The easiest way would be to simply use cin >> s; This will work fine, try it out : )
24th Jan 2019, 10:06 AM
Ipang
+ 3
Ahmad Aziz, sorry for late reply, well since I find it uneasy to explain here, I hope the article link below can provide a better introduction to string. But to answer that a bit, yes, it can be classified as a type, but it's kinda special, because it's a class built with methods and all those necessary tools for basic string manipulation more easily. https://www.cprogramming.com/tutorial/string.html
24th Jan 2019, 3:48 PM
Ipang
+ 1
@lpang Thnx for your answer ....This solution worked, but with small issue that if I entered space between the words the compiler neglect the rest of the input after the first space, and that's why I used cin.get() at the first place.
24th Jan 2019, 10:18 AM
Ahmad Aziz
Ahmad Aziz - avatar
+ 1
okay thnx a lot, but I need to ask a question...is string a data type like int, float, etc... or it's something different?
24th Jan 2019, 11:18 AM
Ahmad Aziz
Ahmad Aziz - avatar
+ 1
Never mind, and thnx alot for your help
24th Jan 2019, 4:20 PM
Ahmad Aziz
Ahmad Aziz - avatar