Does Not Compute

I have to build a calculator that includes a while loop. Please help me out.

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
// Include the iostream library
#include <iostream>
 
//Use the standard namespace
using namespace std;
 
void main ( )
{
   // Declare the variables
   float Number_1;
   float Number_2;
   float Result;
   int Which_Calculation;
   int Answer;
	
	while ( Answer < 2)
	{

   // Give instructions
   cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
   cin >> Which_Calculation;
 
   // Get numbers
   cout << "Please enter the first number." << endl;
   cin >> Number_1;
   cout << "Please enter the second number." << endl;
   cin >> Number_2;
 
   if (Which_Calculation == 1)
   {
	  // Calculate the result
	  Result = Number_1 + Number_2;
   }
   
   if (Which_Calculation == 2)
   {
	  // Calculate the result
	  Result = Number_1 - Number_2;
   }
   
  if (Which_Calculation == 3)
   {
	  // Calculate the result
	  Result = Number_1 * Number_2;
   }
   
   if (Which_Calculation == 4)
   {
	  // Calculate the result
	  Result = Number_1 / Number_2;
   }
   // Print the answer is...
   cout << "The answer is..." << endl;
 
   //Print the result
   cout << Result << endl;
	
   cout <<"Would you like to do something else? Press 1 for yes and 2 for no."<< endl;
	
	system("PAUSE");
	}
	
}
Last edited on

missing ; end of line 13
Which_Calculation is undeclared at line 20
Your testing against Answer which isn't initialised on line 15

There may be other, thats what jumped out.



Last edited on
okay, thank you. as for the which calculation part, it was taken from my original calculator project (the one without the loop) and it work very well. So do I have to initialize it because of the loop?
before you can use a variable you need to declare it, you havent even declared the name or its type such as int Which_Calculation;

Okay, so after I declared Which_Calculation; the errors went away...but now it's saying I need to initialize Answer...?
nevermind, I figured out that part. Although now I can't break the loop....

do a boolean check, i.e. in the menu have an option to quit the calculator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
using namespace std;

int main()
{
	bool running = true;

	// loop will continue while running = true
	do
	{

		// when user wants to quit, set running to false
		// like this
		running = false;

	} while (running);

	return 0;

}
Topic archived. No new replies allowed.