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

  • Context: C/C++ 
  • Thread starter Thread starter firekid123
  • Start date Start date
  • Tags Tags
    Error Function
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
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?
 
Physics news on Phys.org
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*/)) {
  // ...
}
 
Thanks, that worked.