These days I am learning strings and arrays in cpp. In my school they taught us to take a string as user input, one have to use gets() but it isn't working on any of the compilers I have. I have already tried using cstdio library, still errors. I know cin.getline() but it is a bit bigger word.
cin.get()
is the thing you are looking for. However, I recommend using cin, as that is sufficient to use cin >>
. This cin>>
can also be used to input numbers, characters, strings etc.
gets() and puts()
are commonly used in code golf, although they serve the same function as cin>>
and cout<<
. I hope this post helps!
std::cin.get();
is something they teach in beginner classes, hopefully this is the smaller word for getline you were hoping for! :)
In the future, use a program like Visual Studio which has IntelliSense and can list options for you - or just look at the documentation.
The best method would probably be something along these lines:
std::cout << "Input a string: ";
std::string strInput;
std::cin >> strInput;
std::cout << std::endl << "Your string: " << strInput << std::endl;
Good luck!
SEE, 'cin' is used to basically take any input of any data type. BUT. When u input a string using cin it makes an assumption that the string terminates after u enter space. So if u enter 'Hello world' It only reads 'Hello'. When u use gets() it also incorporates spaces into ur input. Syntax: gets(stringName) ; Don't forget to include the header file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.