Question using cerr

So I have a while statement with the parameter (cin >> command) and the while statement is full of if and else if statements for different operations. I have an else at the end to cerr if anything other than an above command is entered.
cerr << "Illegal command <" << command << ">.\n";
I want it to take: "crap these are bad arguments" and produce "Illegal command <crap>."
What do you mean by "bad input"? Are you referring to numbers out of range or incorrect data types? If you're reading a number and need it to be between [a,b] just use a conditional statement to check if the variable is within that range and output to cerr if it is not. If you want to know if completely invalid input was given you can use the state flags of cin to determine that case. The following links will be helpful.

http://www.cplusplus.com/reference/ios/ios/good/
http://www.cplusplus.com/reference/ios/ios/clear/
http://www.cplusplus.com/reference/istream/istream/sync/
I want to take the string input "crap these are bad arguments" and produce the cerr "Illegal command <crap>." I just want the first word, which is the command, the rest is the argument in which I don't need for this else statement.
1
2
3
4
5
6
7
8
9
10
#include <limits>
//...
std::string command;
std::cin >> command; //Read a single word (command)
if(is_valid(command)) { //check command using user defined function
    //...
} else {
    std::cerr << "Illegal command <" << command << ">.\n"; //output error message
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Skip everything till the end of line
}
Topic archived. No new replies allowed.