exit function

Pages: 12
Sep 15, 2018 at 5:22pm
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

thank you
Sep 15, 2018 at 5:25pm
also here is case 3



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
Sep 15, 2018 at 5:40pm
cin >> hex >> x; will allow you to enter the number as hex.
Sep 15, 2018 at 5:42pm
thats awesome, ty!
Sep 15, 2018 at 5:46pm
repeater any thoughts on the exit function question up top?
Sep 15, 2018 at 5:48pm
You can call exit(0) if you like. It's bad practice and generally frowned up. The proper way to end the program is to return from the main function.
Sep 15, 2018 at 6:08pm
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
Sep 15, 2018 at 6:20pm
There is an actual function called exit, so I'd guess that one.

https://en.cppreference.com/w/cpp/utility/program/exit
Sep 15, 2018 at 6:21pm
While I'm here, void main() is wrong. main returns an int in C++.
Sep 15, 2018 at 6:27pm
thanks, he uses void in all examples so... appreciate all your help!!
Sep 15, 2018 at 6:29pm
Well, now you know. Why not try switching it for correct C++ and see what happens?
Sep 15, 2018 at 6:35pm
yeah, teacher uses void main() for everything so far, so that's what I'm using. Thanks for the help.
Sep 15, 2018 at 6:49pm
closed account (E0p9LyTq)
teacher uses void main()

1. What compiler are you using?

2. When you use cout do you #include <iostream> or #include <iostream.h>?
Sep 15, 2018 at 7:49pm
visual studio, <iostream>
Sep 15, 2018 at 8:19pm
Oh, that is a surprise. Usually when someone's teacher advocates void main() they're using Turbo-C++ from almost thirty years ago.
Sep 15, 2018 at 8:49pm
I'll let him know your thoughts lol.....definitely gonna find out his reasoning
Sep 15, 2018 at 9:29pm
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.

Last edited on Sep 15, 2018 at 9:30pm
Sep 15, 2018 at 9:40pm
The main function returns int.

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.
Sep 17, 2018 at 4:14pm
closed account (E0p9LyTq)
@witchee666,

Visual Studio, even the latest VS2017, compiles void main(). It issues a warning, though.

It makes me wonder what other aspects of C++ your instructor teaches that is outdated, wrong and potentially detrimental to the students.
Sep 17, 2018 at 5:27pm
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.

Pages: 12