简体   繁体   中英

How do you get input until a newline in C++?

I'm reading input using cin . If I leave the input blank (and just hit enter), the cursor moves to a new line and asks for input again. Is there any way to make cin or scanf just return an empty string in this case?

Instead of operator>> , use getline .

std::string data;
std::getline(std::cin, data);

Side note: There's no recursion involved here. Recursion is when a function calls itself, that's not happening here.

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