Oct 31, 2010 at 2:21am UTC
How to properly use cerr and exit(int).
For instance, using an if else statment, say the else is exit(1). How to tell the compiler to write that 1 to a file so I can see the error code?
and cerr for output errors, How is this used properly?
IE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
cout << "\n\nEnter a number: "
cin >> num1;
if (cin.good())
cout << num1;
else
exit(1);
return 0;
}
OR:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
int main()
{
int num1 = 0;
cout << "\n\nEnter a number: "
cin >> num1;
if (cin.good())
{
cout << num1;
}
else
{
cerr << "\n\n.......INVALID INPUT\n....Exiting...\n\n" ;
exit(1);
}
return 0;
}
Thanks in advance!
Last edited on Oct 31, 2010 at 2:30am UTC
Nov 1, 2010 at 12:47am UTC
Thanks for the help. So lets say im in windows, and I compile a program and it ends up executing exit(1).
//If i type echo %errorlevel%, does that mean the command line should be in the same directory as my cpp file?
edit: nm I figured it out.
Is there a way to have all cerr output logged to a txt file?
Last edited on Nov 1, 2010 at 12:52am UTC