How to take input in cpp in variable and after how to print that ? I tried it but its print only one world | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

11th Dec 2022, 12:48 PM
Girish Sahu
Girish Sahu - avatar
2 Answers
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)
11th Dec 2022, 6:14 PM
technocat
technocat - avatar
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.
13th Dec 2022, 11:50 AM
Bloody Halos
Bloody Halos - avatar