+ 1
How to take input in cpp in variable and after how to print that ? I tried it but its print only one world
Like input is twenty but its print only t , why?
2 odpowiedzi
0
#include <iostream>
using namespace std ;
int main() {
string x;
cin>>x;
cout <<x;
return 0;}
if You want to input text with a space, You should use getline instead of cin(i don't know it well, ask Google)
0
If you put just twenty as input and only output the 't', then assume your variable type was a char as that is what I got when just tried
So first to get more than one char, a whole word say, you need your variable to be of type string like technocat showed.
Cin only takes up to a space in the face of a big string. Ex, if you put "hello, world" , it will output only "hello,".
To get a bigger string, as technocat also said, getline helps with that. Syntax getline(cin, variable name).
There is a third parameter to it that is defaulted to '\n' for a new line. So it will read up to that. Third parameter is called a delimiter. You can put a different char in its place to override the default.
Think there might be another way to use it too, but I'm a bit rusty.