Write your question here.
im giving user 4 options and using switches inside switches for other choices. choosing option 4 is supposed to exit program and my instructions are to use the exit function....not really understanding...is it exit(0)?
void main()
{
int x,y,z;
float a;
bool t;
cout << "what would you like to do?\n" <<
"enter 1 to find cosines \n" <<
"enter 2 to find logs\n" <<
"enter 3 to convert between decimals and hexadecimals\n" <<
"enter 4 to EXIT program" << endl;
cin >> x;
this is 1st part ...how do i use exit function in case 4
case 3:
cout << "Enter 1 to convert from decimal to hexadecimal\n" <<
"Enter 2 to convert from hexadecimal to decimal\n" << endl;
cin >> z;
switch (z)
{
case 1 :
cout << "Lower or upper case for hex print out?\n" <<
"enter 'true' for upper or 'false' for lower" << endl;
cin >> boolalpha >> t;
switch (t)
{
case true:
cout << "enter a whole number to be converted "<< endl;
cin >> x;
cout << "the number " << x << " as a hexadecimal uppercase is " <<
showbase << uppercase << hex <<x<< endl;
break;
case false:
cout << "enter a whole number to be converted " << endl;
cin >> x;
cout << "the number " << x << " as a hexadecimal lowercase is " <<
showbase << hex << x << endl;
break;
}
break;
case 2 :
cout << " enter a hexadecimal number" << endl;
cin >> x;
cout << " the number as a decimal is "<<showbase << dec << x << endl;
break;
everything works great but my input for hex needs to be 5d.....since x is an int i think it only reads the 5....what kind of variable do i store hex in to convert to dec
so my assignment reads "If the user picks the exit(case 4), use the actual exit function to end the program. "
i guess im confused as to what "the actual exit function" is
As I recall, in the C++ 1998 definition document, section 3.6.1.2 states
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.
Pretty clear. The main function returns int. Later C++ standards (03, 11, 14, 17) say the same.
While we're here, the very same 3.6.1 section talks about using exit to end rather than returning from main:
Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4).
It's certainly valid to do so, but as the standard makes clear, in doing so, you're abandoning the proper destruction of any automatic variables; if their destructors were doing anything you're relying on, that could be a problem.
Furthermore, the returned value (via return or via exit() ) is preferably in range [0..127]. The 0 is considered "success" and the other values some error. The program can thus report many different errors.
Visual Studio, even the latest VS2017, compiles void main(). It issues a warning, though.
That's an improvement, VS didn't even used to warn about this issue because they didn't want to "break" old code that used this "feature".
Furthermore, the returned value (via return or via exit() ) is preferably in range [0..127].
Actually the only standard defined values for the exit() function are zero, EXIT_FAILURE, or EXIT_SUCCESS anything else is implementation defined. However most of the current compilers (for Hosted Environments) do allow returning other values. For example the current gcc compiler allows returning values in the range of 0 to 255, it appears to use normal unsigned rounding for values outside this range. I have no idea what VS or Clang allow but I do believe that they allow at least the 0 ..127 values.