Basic Calculator

Basic calculator that allows users to select which operation they want to perform and then prompted to make a decision on whether or not they want to perform another calculation. The problem that I am having is after the user performs their desired operation, they cannot enter a option as to whether or not they want to repeat the program. After the answer to their calculation is shown, the messages "ERROR. Please make a valid selection\n" and "Would you like to perform another operation? Yes (Y) or No (N)" along with the menu and the answer to the calculation are displayed and re-posted in an infinite loop (not sure if there is a more technical term for this) which is why the user cannot enter a choice if they want to repeat the program or not. I have seen other basic/novice calculators but none that are similar to mine and cannot find a solution to my problem. Can anyone tell me what I am doing wrong or point me in the right direction to something that would be helpful?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

string line(40, '*'), name;

char option, option2, menu(char &option), restart(char &option2);

double number, base, exponent, answer;

void intro(), description(), calculation(double &answer);

int main()
{
	intro();
	cout << setw(60) << "Please enter your name and press <ENTER>.\n" << endl;
	getline(cin, name);
	cout << setw(36) << "Hi, " << name << "!\n" << endl;
	description();
	menu(option);
	calculation(answer);
	restart(option2);
	
	return 0;
}

void intro()
{
	cout << setw(58) << line << endl;
	cout << setw(60) << "Welcome to The Basic Calculator (version 1.0)" << endl;
	cout << setw(58) << line << endl;
}

void description()
{
	cout << setw(14) << name << ", please select which of the following operations you\n" 
							 << setw(58) << "would like to perform by typing the\n" 
							 << setw(60) << "letter in parenthesis(NOT case sensitive) and press <ENTER>.\n" << endl;
}

char menu(char &option)													//Menu that lets user choose what operation they want to perform. It stores there character (A,S,M,D,P,Q,E) selection as "option" and then returns the value to int main(). 
{
   cout << setw(45) << "Addition (A)\n"
		<< setw(45) << "Subtraction (S)\n"
		<< setw(45) << "Multiplication (M)\n"
		<< setw(45) << "Division (D)\n"
		<< setw(45) << "Power (P)\n"
		<< setw(45) << "Square Root (Q)\n"
		<< setw(45) << "Exit Basic Calculator (E)\n" << endl;

   cin >> option;
   return option;
}

void calculation(double &answer)											//Performs all calculations based on user selection. It stores the answer , "answer" and then returns the value to int main().
{
	if (option == 'A' || option == 'a')										// The "||" is the or operator. The if statements means that the addition selection is NOT case sensitive. 
	{
		cout << "Please enter the numbers you would like to add separated by a space, then press CTRL + Z (if using Windows OS) or CONTROL-D (if using Mac OS), and then hit <ENTER>" << endl;	//ctrl-z nd ctrl-d indicate the end-of-file which tells the computer that the user is finished entering values
	
		while (cin >> number)											//User can enter as many number as they choose.
		{
			answer += number;											//Every number that the user enters will be added together, the final result is the 'answer'
		}
		cout << answer << endl;											//MUST be outside of the while loop because if not, output will display each solution after it adds 1 pair of numbers. Having it outside the loop only displays the Final result
	}

	if (option == 'S' || option == 's')
	{
		cout << "Please enter the numbers you would like to subtract separated by a space, then press CTRL + Z (if using Windows OS) or CONTROL-D(if using Mac OS), and then hit <ENTER>" << endl;

		while (cin >> number)
		{
			answer -= number;
		}
		cout << answer << endl;
		
	}
	
	if (option == 'M' || option == 'm')
	{
		cout << "Please enter the numbers you would like to multiply separated by a space, then press CTRL + Z (if using Windows OS) or CONTROL-D(if using Mac OS), and then hit <ENTER>" << endl;

		while (cin >> number)
		{
			answer *= number;
		}
		cout << answer << endl;
		
	}

	if (option == 'D' || option == 'd')
	{
		cout << "Please enter the numbers you would like to divide separated by a space, then press CTRL + Z (if using Windows OS) or CONTROL-D(if using Mac OS), and then hit <ENTER>" << endl;

		while (cin >> number)
		{
			answer /= number;
		}
		cout << answer << endl;
		
	}

	if (option == 'P' || option == 'p')
	{
		cout << "Please enter the base, hit <ENTER>, then the exponent and then hit <ENTER> again" << endl;

		cin >> base;
		cin >> exponent;

		answer = pow(base, exponent);

		cout << answer << endl;
		
	}

	if (option == 'Q' || option == 'q')
	{
		cout << "Please enter the number and then hit <ENTER>" << endl;

		cin >> number;
		
		answer = sqrt(number);

		cout << answer << endl;
		
	}

	if (option == 'E' || option == 'e')
	{
		cout << "Thank you for using the Basic Calculator version 1.0" << endl;
		exit(1);
	}
	
	else														//If user does not enter a appropriate selection, the menu will re-appear
	{
		cout << "ERROR. Please make a valid selection\n" << endl;
		menu(option);
		calculation(answer);
		restart(option2);
	}

}

char restart(char &option2)
{
	cout << "Would you like to perform another operation? Yes (Y) or No (N)" << endl;

	cin >> option2;
	return option2;

	if (option2 == 'Y' || option2 == 'y')
	{
		menu(option);
		calculation(answer);
		restart(option2);
	}

	if (option2 == 'N' || option2 == 'n')
	{
		cout << "Thank you for using the Basic Calculator version 1.0" << endl;
		exit(1);
	}

	else																//If user does not enter a appropriate selection, the menu will re-appear
	{
		cout << "ERROR. Please make a valid selection\n" << endl;
		restart(option2);
	}
}
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 doubles 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.
Topic archived. No new replies allowed.