Hi & welcome to cplusplus :+)
You can use a
bool
type variable to control a
while
loop:
1 2 3 4 5 6 7 8
|
bool Quit = false;
while (!Quit) {
// your code here
// put the switch or if else chain in here
}
| |
Some other things:
Try to avoid line 6, Google to see why, and what to do instead - hint
std::cout
Avoid global variables - they easily lead to problems, and are bad unless you know what you are doing. Declare them in
main()
at least. It best to keep the scope of variables as local as possible.
I like to declare and initialise my variables 1 per line, because this achieves another golden rule - always initialise your variables to something, note that zero is not always the best choice. Write comments about each variable, explaining what ranges of values are valid. With functions write a few lines of comments about what the function does, include what the pre and post conditions are. Google to find out what these mean.
Declare your functions 1 per line as well, - it is just easier to read that way.
The menu function takes a reference to
option
and returns that same variable as well (which you don't make use of the function was called)- maybe that function should be
void
?
Try to make use of the
toupper
function - it will make your
if
statements easier :
if (option2 == 'N')
have a go at naming your variable a bit better,
option
and
option2
are not so flash - options for what ? If you name your variables and functions well the code should tell a story of what is happening.
Another way to do things is to use a
switch
statement instead of the
if
else
chain. Make sure to have a
default:
case, just like you have an else, to catch bad input. Have each
case
call a function (keeps things tidy) - you could do the same for each of your if statements as the code stands now .
Why does everyone forget to check for division by zero ? :+) One has to be careful doing this for
double
s and
float
- they aren't exact because of the way they are represented. So you need to set some suitable precision value 1e-3 or 1e-6 say - make it
const
. If the absolute value of your number is less than this precision value, then the number effectively for your purposes "is zero". A similar idea is used to compare other numbers for equality.
You need to do some checks for the
exponent
and
sqrt
function - one can't have
sqrt
of negative numbers, there are other combinations that produce inf or Nan. It is probably better to validate the numbers before the function is called, rather than try to catch errors produced by the math functions.
Btw exit(1) is a drastic step and means abnormal program termination.
Hope all this helps a bit :+) and good luck. I (& others too) look forward to seeing your new code.