简体   繁体   中英

When entering a string in the console. Only the first word is then printed

So I create a string:

 string randomstring;

Then I enter something in the console of what it should contain

"A broken string"

And now when I type:

  cout << randomstring << endl;

The output reads

A

Why doesn't it prints the whole string ?

Use std::getline() when you take a string input from the user.

std::string str;
std::getline(std::cin, str);
std::cout << str << std::endl;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM