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.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top