开发者

check if std::cin operator>> has read the whole line

开发者 https://www.devze.com 2023-01-11 22:10 出处:网络
I\'m implementing a little command line parser. Let\'s say I have a command that requires 2 parameters. I want to let the user type all 3 strings (the command and 2 parameters) on one line, as well as

I'm implementing a little command line parser. Let's say I have a command that requires 2 parameters. I want to let the user type all 3 strings (the command and 2 parameters) on one line, as well as on several lines. Currently I'm having something like this:

std::string command;
std::cin >> command;
std::cout << command << " entered\n";
std::string param1;
std::cin >> param1;
std::cout << param1 << " entered\n";
std::string param2;
std::cin >> param2;
std::cout << param2 << " entered\n";

Now I want to be able to detect that the user has just entered the command without any parameter, and output directions开发者_JAVA技巧 for that. I think after getting the command I should test if the line contains anything else and if it doesn't, ask the user to type more. I have tried with eof() and fail() but they do not work. How can I check for that then?

Thanks.


If you want to read a line, then you should use std::getline. Once you have the whole line, you can break it up into words, however many there are.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号