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

  • Thread starter Thread starter firekid123
  • Start date Start date
  • Tags Tags
    Error Function
AI Thread Summary
To handle errors in a function and print them in the main function, the return value of the function should be checked using an if statement. If the function indicates an error with a specific return value, the main function can print an error message using cerr. For a more robust solution that doesn't require checking return values for every function call, exception handling using throw and catch statements is recommended. This allows for a cleaner error management approach.
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?
 
Technology 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.
 
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top