C/C++ How do I get main to print out an error from a function?

  • Topic: C/C++ 
  • Thread starter firekid123
  • Start date
  • Tags Tags
    Error Function
3 replies · 3K views · last reply
firekid123
Messages
8
Reaction score
0
I'm having trouble with printing out an error with cerr in the main function when there's an error in another function.


Example:

int main (){
//body
//statements

function (//parameters);
cerr<<"there's a problem"<<endl;
}
int function (//parameters){
if a < b
return -1;
else
return 0;
}

How would I get main to print out the error of the function?
 
If the return value of the function is what indicates that there was an error, you need to check the return value in an if statement.
Code:
if (/*error value*/ == function(/*parameters*/)) {
  // ...
}
 
For a more general way where you don't have to remember to check the return value from every function call, find out about exception handling and the throw and catch statements.