Help needed for Basic Calculator

I need help with this. I am brand new to coding and I am trying to make a calculator but it keeps infinitely looping after it asks to if you want to make another question. If someone could show me what I am doing wrong or any tips in general please do.


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
 1 #include <iostream>
  2  
  3  using namespace std;
  4  
  5  #include <stdlib.h>
  6  
  7  int main()
  8  {
  9     int a;
 10     int b;
 11     int x;
 12     int z;
 13     int y;
 14     char Operator;
 15     char Problem;
 16  
 17    do{
 18        do{
 19     cout << "\nInput any number: ";
 20      cin >> a;
 21     cout << "\nChoose an operation ( + - * / or X for Exit and C for Clear): ";
 22      cin >> Operator;
 23        }while(Operator=='C');
 24  
 25          switch(Operator)
 26          {
 27        case '+':
 28          cout << "\nEnter second number: ";
 29          cin >> b;
 30          z = a + b;
 31          cout << "\nThe sum of those numbers is: " << z << endl;
 32          break;
 33        case '-':
 34          cout << "\nEnter the second number: ";
 35          cin >> b;
 36          z = a - b;
 37          cout << "\nThe difference of those numbers is: " << z << endl;
 38          break;
 39        case '*':
 40          cout << "\nEnter the second number: ";
 41          cin >> b;
 42          z = a * b;
 43          cout << "\nThe product of those numbers is: " << z << endl;
 44          break;
 45  
 46        case '/':
 47          cout << "\nEnter a second number: ";
 48          cin >> b;
 49            if(b==0)
 50              {
 51                 cout << "Invalid Divisor, please enter a new value: ";
 52                 cin >> b;
 53              }else
 54          z = a / b;
 55          y = a % b;
 56        if(a % b > 0){
 57                       cout << "\nThe quotient of those numbers is: " << z << " and " << y << " Remainder" << endl;
 58                     }else
 59          cout << "\nThe quotient of those numbers is: " << z << endl;
 60          break;
 61  
 62        case '^':
 63          z = a * a;
 64          cout << "\nThis number squared is: " << z << endl;
 65          break;
 66  
 67        case 'C':
 68        case 'c':
 69          break;
 70  
 71        case 'X':
 72        case 'x':
 73          terminate();
 74          break;
 75             }
 76  
 77          if(z>=-2000000000000){
 78           cout << "Would you like to start another problem?: ";
 79           cin >> Problem;
 80  
 81          switch(Problem)
 82            {
 83        case 'Yes' :
 84        case 'yes' :
 85          cout << "Previous answer was: " << z;
 86              if(y>0){
 87                  cout << " and " << y << " Remainder" << endl;
 88                     }
 89          break;
 90  
 91        case 'No' :
 92        case 'no' :
 93          terminate();
 94          break;
 95            }
 96          }
 97       }while(z>=-200000000000);
 98    return 0;
 99  }
Last edited on
Could you please Edit your post and put it all between [code]code tags[/code]
http://www.cplusplus.com/articles/jEywvCM9/
Ok, did it. Sorry I didnt already have it in the right format.
1
2
3
4
5
6
7
 do{
 18        do{
 19     cout << "\nInput any number: ";
 20      cin >> a;
 21     cout << "\nChoose an operation ( + - * / or X for Exit and C for Clear): ";
 22      cin >> Operator;
 23        }while(Operator=='C');


your do-while statement closes with a while statement that is set to TRUE (==). That's my opinion.

If im not right about the first comment I made. Im pretty sure Im "just" on this one...It's always good to initialize your local variables to 0

int a = 0, b = 0, x = 0, y = 0, z = 0, etc
Last edited on
The way you check whether you should continue or not is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (z >= -2000000000000) {
			cout << "Would you like to start another problem?: ";
			cin >> Problem;

			switch (Problem) {
			case 'Yes':
			case 'yes':
				cout << "Previous answer was: " << z;
				if (y>0) {
					cout << " and " << y << " Remainder" << endl;
				}
				break;

			case 'No':
			case 'no':
				terminate();
				break;
			}
		}
	} while (z >= -200000000000);

Don't use that int variable, instead declare new bool var to check if you should continue. Also you cannot store string in char variable. You can store them in char arrays, but for beginners it could be easier to use string objects. You can also store string in std::string object and then check it in do-while loop, without using bool variables. Last advise, you should use 'a' for chars, but "abc" for string 'cuz it adds null character at the end of string.
Last edited on
Topic archived. No new replies allowed.