I am making a function that takes a number from the user\'s input and finds the absolute value of it. I want to make it return an error if the user inputs anything other than j
In addition to the great answers here, you could try using std::stringstream:
http://cplusplus.com/reference/iostream/stringstream/stringstream/
It works like any other stream for the most part, so you could do something like:
int converted;
string user_input;
cin >> user_input;
stringstream converter(user_input);
if(!(converter >> converted)) {
cout << "there was a problem converting the input." << endl;
}
else {
cout << "input successfully converted: " << converted << endl;
}
HTH!
P.S. personally, I would just use boost::lexical_cast<>, but for a homework assignment you probably won't have boost available to you. If you become a professional C++ programmer, Boost will become one of your best friends outside of the STL.