String input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

String input

Can i take input from user string values with space, if i can than how

12th Jun 2023, 12:07 PM
MMK
MMK - avatar
11 Answers
+ 5
You can use getline() string inp; getline(cin, inp);
12th Jun 2023, 12:14 PM
Lisa
Lisa - avatar
+ 3
#include <iostream> #include <string> int main() { std::string input; std::cout << "Enter string: "; std::getline(std::cin, input); std::cout << "You entered : " << user_input << std::endl; return 0; }
13th Jun 2023, 6:34 PM
Vaibhav
Vaibhav - avatar
+ 2
difference between cin and getline(cin, input)... cin take the value when a space is reached. getline(cin) gets the entire line as a string. #include <iostream> using namespace std; int main() { string address; getline(cin, address); cout << address << '\n'; int n1, n2, n3; cin >> n1 >> n2 >> n3; cout << n1 << endl; cout << n2 << endl; cout << n3 << endl; } /* Example input 221B Baker Street 7 11 42 submit */
12th Jun 2023, 12:41 PM
Bob_Li
Bob_Li - avatar
+ 2
using C++ to program an AI is not a beginner project....
12th Jun 2023, 12:59 PM
Bob_Li
Bob_Li - avatar
+ 1
Is that will allow to use spase
12th Jun 2023, 12:26 PM
MMK
MMK - avatar
+ 1
Wow it’s work thanks aging Lisa...
12th Jun 2023, 12:36 PM
MMK
MMK - avatar
+ 1
Ya ya i get it man, thanks for your kindness....
12th Jun 2023, 12:47 PM
MMK
MMK - avatar
+ 1
actually I am trying to make simple AI. if I learn something then I put it in that....
12th Jun 2023, 12:49 PM
MMK
MMK - avatar
+ 1
yeah but I like difficulty... if I learn something new then I added I am just writing something in future maybe it work
12th Jun 2023, 1:02 PM
MMK
MMK - avatar
+ 1
if you want your user to be able to add spaces in their input with characters, try using the getline() function, here is an example of how to use it. #include <iostream> #include <string> int main() { std::string input; std::getline(std::cin, input); std::cout << input; return 0; } the line of code that says std::getline(std::cin, input) gets the users console input, then it will print it out at the end! I hope that this helped you!
13th Jun 2023, 11:13 PM
EBU Okeoma
EBU Okeoma - avatar
0
do you have any suggestion for me...
12th Jun 2023, 12:53 PM
MMK
MMK - avatar