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

  • C/++/#
  • Thread starter firekid123
  • Start date
  • #1

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
diazona
Homework Helper
2,175
6
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*/)) {
  // ...
}
 
  • #3
Thanks, that worked.
 
  • #4
AlephZero
Science Advisor
Homework Helper
6,994
291
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.
 

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

Replies
13
Views
6K
Replies
8
Views
606
Replies
9
Views
1K
Replies
3
Views
4K
  • Last Post
Replies
4
Views
2K
Replies
14
Views
4K
  • Last Post
Replies
4
Views
2K
Top