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
Have you checked out atoi, or the even better strtol? I recommend starting there.
If you are actually trying to program in idiomatic C++, ignore the (intentionally?) bad advice you are being given. Especially the answers pointing you toward C functions. C++ may be largely backwards-compatible with C, but its soul is a totally different language.
Your question is so foundational as to make for a terrible homework assignment. Especially if you're so adrift that you don't know to avoid conio.h and other tragedies. So I'm just going to write out a solution here:
#include <iostream>
#include <string>
// Your function is presumably something like this
// although maybe you are just using integers instead of floats
float myAbs(const float x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
int main(int argc, char* argv[]) {
// give a greeting message followed by a newline
std::cout << "Enter values to get |value|, or type 'quit'" << std::endl;
// loop forever until the code hits a BREAK
while (true) {
// attempt to get the float value from the standard input
float value;
std::cin >> value;
// check to see if the input stream read the input as a number
if (std::cin.good()) {
// All is well, output it
std::cout << "Absolute value is " << myAbs(value) << std::endl;
} else {
// the input couldn't successfully be turned into a number, so the
// characters that were in the buffer that couldn't convert are
// still sitting there unprocessed. We can read them as a string
// and look for the "quit"
// clear the error status of the standard input so we can read
std::cin.clear();
std::string str;
std::cin >> str;
// Break out of the loop if we see the string 'quit'
if (str == "quit") {
break;
}
// some other non-number string. give error followed by newline
std::cout << "Invalid input (type 'quit' to exit)" << std::endl;
}
}
return 0;
}
This lets you use the natural abilities of the iostream classes. They can notice when they couldn't automatically convert what a user entered into the format you wanted, and give you a chance to just throw up your hands with an error -or- try interpreting the unprocessed input a different way.
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.
Treat user input as std::string
or char *
, then validate whether it contains a valid digit character.
Pass your number as a reference and return an error code. Using the function argument as an output parameter.
bool parseNumber(int &n)
{
...
//assign to number to n
// if number parsing is ok return true;
...
return false;
}
int main()
{
int number=0;
if(!parseNumber(number))
std::cout << "Number parsing failed\n";
}