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

In summary, the conversation discusses how to print out an error in the main function using cerr when there is an error in another function. One suggestion is to check the return value of the function in an if statement, while another option is to use exception handling and the throw and catch statements for a more general approach.
  • #1
firekid123
8
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
  • #2
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
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.
 
  • #5


There are a few ways to get main to print out an error from a function. One way is to use the return value of the function to indicate whether an error occurred. For example, in the function provided in the question, you can use the return value of -1 to indicate an error and 0 to indicate no error. Then in the main function, you can check the return value of the function and print out an error message using cerr if the value is -1.

Another way is to use an exception handling mechanism, such as try-catch blocks. This allows you to catch any errors that occur in the function and handle them in the main function. For example, you can use a try block in the main function and catch any exceptions thrown by the function, then print out the error message using cerr in the catch block.

You can also pass a reference to a variable in the main function to the function and modify its value in the function if an error occurs. Then in the main function, you can check the value of the variable and print out an error message using cerr if it indicates an error.

Overall, the best approach will depend on the specific needs and structure of your code. It's important to handle errors in a way that is clear and easily readable for other programmers who may need to work with your code.
 

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

1. How do I call a function in C/C++?

To call a function in C/C++, you need to declare the function prototype and then use the function name followed by parentheses and any necessary arguments. For example:
int add(int a, int b);
int result = add(5, 3);

This will call the function "add" with the arguments 5 and 3, and store the result in the variable "result".

2. How do I print an error message from a function in C/C++?

To print an error message from a function in C/C++, you can use the standard library function printf(). This function takes in a format string and any necessary arguments. For example:
printf("Error: Cannot divide by zero.");
This will print the error message "Error: Cannot divide by zero." to the console.

3. How do I handle errors in C/C++?

In C/C++, error handling can be done by using conditional statements such as if and else, as well as try-catch blocks. You can also use the errno global variable to check for errors after a function call. Additionally, you can use the assert() macro to check for conditions and terminate the program if they are not met.

4. How do I return an error code from a function in C/C++?

In C/C++, you can use the return statement to return an error code from a function. You can choose to return a specific integer value or use the errno global variable to return the error code. It is important to properly handle the returned error code in the calling function.

5. How do I debug errors in my C/C++ code?

To debug errors in your C/C++ code, you can use a debugger such as gdb or lldb. These tools allow you to step through your code and track the values of variables to identify any errors. You can also use printf() statements to print out values and track the flow of your program. Additionally, using a coding style guide and writing clear and concise code can help prevent errors from occurring.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
2
Views
704
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
3
Replies
70
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
6
Views
947
Back
Top