### What is the use of cerr?

In the following Code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148`` ``````// EX6_10.cpp // A program to implement a calculator #include // For stream input/output #include // For the exit() function #include // For the isdigit() function using std::cin; using std::cout; using std::cerr; using std::endl; void eatspaces(char* str); // Function to eliminate blanks double expr(char* str); // Function evaluating an expression double term(char* str, int& index); // Function analyzing a term double number(char* str, int& index); // Function to recognize a number const int MAX(80); // Maximum expression length, // including '\0' int main() { char buffer[MAX] = {0}; // Input area for expression to be evaluated cout << endl << "Welcome to your friendly calculator." << endl << "Enter an expression, or an empty line to quit." << endl; for(;;) { cin.getline(buffer, sizeof buffer); // Read an input line eatspaces(buffer); // Remove blanks from input if(!buffer[0]) // Empty line ends calculator return 0; try { cout << "\t= " << expr(buffer) // Output value of expression << endl << endl; } catch( const char* pEx) { cerr << pEx << endl; cerr << "Ending program." << endl; return 1; } } } // Function to eliminate spaces from a string void eatspaces(char* str) { int i(0); // 'Copy to' index to string int j(0); // 'Copy from' index to string while((*(str + i) = *(str + j++)) != '\0') // Loop while character // copied is not \0 if(*(str + i) != ' ') // Increment i as long as i++; // character is not a space return; } // Function to evaluate an arithmetic expression double expr(char* str) { double value(0.0); // Store result here int index(0); // Keeps track of current character position value = term(str, index); // Get first term for(;;) // Indefinite loop, all exits inside { switch(*(str + index++)) // Choose action based on current character { case '\0': // We're at the end of the string return value; // so return what we have got case '+': // + found so add in the value += term(str, index); // next term break; case '-': // - found so subtract value -= term(str, index); // the next term break; default: // If we reach here the string is junk char message[38] = "Expression evaluation error. Found: "; strncat_s(message, str + index - 1, 1); // Append the character throw message; break; } } } // Function to get the value of a term double term(char* str, int& index) { double value(0.0); // Somewhere to accumulate // the result value = number(str, index); // Get the first number in the term // Loop as long as we have a good operator while(true) { if(*(str + index) == '*') // If it's multiply, value *= number(str, ++index); // multiply by next number else if(*(str + index) == '/') // If it's divide, value /= number(str, ++index); // divide by next number else break; } return value; // We've finished, so return what // we've got } // Function to recognize a number in a string double number(char* str, int& index) { double value(0.0); // Store the resulting value // There must be at least one digit... if(!isdigit(*(str + index))) { // There's no digits so input is junk... char message[31] = "Invalid character in number: "; strncat_s(message, str+index, 1); // Append the character throw message; } while(isdigit(*(str + index))) // Loop accumulating leading digits value = 10*value + (*(str + index++) - '0'); // Not a digit when we get to here if(*(str + index) != '.') // so check for decimal point return value; // and if not, return value double factor(1.0); // Factor for decimal places while(isdigit(*(str + (++index)))) // Loop as long as we have digits { factor *= 0.1; // Decrease factor by factor of 10 value = value + (*(str + index) - '0')*factor; // Add decimal place } return value; // On loop exit we are done }``````

On line 43 & 44 what is the use of cerr? I can use cout and it compiles perfectly fine. Is it there just to tell another programmer it outputs a error rather then just normal output? If not why? Also is there any functionality difference between cout cerr and clog?

May someone please explain this to me? Thank you.

Also it takes about needing that separate header file for the "exit function"? Where is this function in the code? o.O
Last edited on
http://stackoverflow.com/a/2404545
Main idea that you can redirect streams. So you can have cout on your screen, clog in some file, and cerr displayed on your screen and written to a file. Or printed. Or sent to logging server. Or something

 separate header file for the "exit function"? Where is this function in the code?
Well looks like he didn't need it.
But it says at the top he added this:

`#include <cstdlib> // For the exit() function `

What function. Its nowhere to be found...
That's exactly it. It isn't there, so it was probably something that was changed and the comment was never updated.
I see thank you very much.

But the thing is its from my book "Ivor Horton's Beginning C++ 2012"

Although I have found many typos in the book, this is just a fail. f7
Last edited on
Topic archived. No new replies allowed.