+ 1

How to user input a string variable?

This is my code:- 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int main() { 6 7 string myString; 8 cout << "Enter String:" <<"\n"; 9 10 return 0; } Now my question is what should i write in line number 9 to take a string input from the user so that I can perform operations on it. Also what should be the command to display the output string.

28th Oct 2017, 12:25 PM
Designex Rockx
Designex Rockx - avatar
3 Respostas
+ 11
Just adding to @Manual's answer, if you want to take an entire line of input (including whitespaces, terminated by newline), use getline(). string myString; cout << "Enter String : "; getline(cin, myString);
28th Oct 2017, 1:46 PM
Hatsy Rei
Hatsy Rei - avatar
28th Oct 2017, 12:47 PM
Manual
Manual - avatar
+ 1
@Designex Put the code in the playground Use (Console in) Cin >> on the value you want the input to go to.
28th Oct 2017, 12:29 PM
Manual
Manual - avatar