Trouble with a Calculator I just finished.

The program works fine until you access case 7: on line 64. Once you do that, no matter what you input into the program, it quits.
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
//***CALCULATOR***
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main(){
    int input;
    double x, y, b, h, s, base_one, base_two, l, w;

    beginning:
    cout << "**MAIN MENUE** \n";
    cout << "1.  multiplication \n";
    cout << "2.  devision \n";
    cout << "3.  addition \n";
    cout << "4.  subtraction \n";
    cout << "5.  square \n";
    cout << "6.  Square root \n";
    cout << "7.  formulas \n";
    cout << "select calculation type or press '8' to quit: \n";
    cin >> input;
    switch (input){

    case 1:
    cout << "please enter two numbers to be multiplied.  press ENTER between each integer:";
    cin >> x;
    cin >> y;
    cout << "the product of " << x << " and " << y << " is " << x*y;
    goto beginning;

    case 2:
    cout << "please enter two numbers to be devided.  Press ENTER between each integer: ";
    cin >> x;
    cin >> y;
    cout << x << " devided by " << y << " is " << x/y;
    goto beginning;

    case 3:
    cout << "please enter two numbers to be added.  Press ENTER between each integer: ";
    cin >> x;
    cin >> y;
    cout << "the sum of " << x << " and " << y << " is " << x+y;
    goto beginning;

    case 4:
    cout << "please enter two numbers to be subtracted.  Press ENTER between each integer: ";
    cin >> x;
    cin >> y;
    cout << "the difference between " << x << " and " << y << " is " << x-y;
    goto beginning;

    case 5:
    cout << "please enter a number to be squared and press ENTER: ";
    cin >> x;
    cout << x << " squared is " << x*x << ".";
    goto beginning;

    case 6:
    cout << "please enter a number to find it's square root and press ENTER: ";
    cin >> x;
    cout << "the square root of " << x << " is " << sqrt(x) << ".";
    goto beginning;

    case 7:
        int another_input;

        cout << "**FORMULAS** \n";
        cout << "1.  area \n";
        cout << "2.  volume \n";
        cout << "select formula type: ";

        switch(another_input){
            case 1:
            int yet_another_input;

            cout << "**AREA** \n";
            cout << "1.  triangle \n";
            cout << "2.  square \n";
            cout << "3.  rectangle \n";
            cout << "4.  parallelogram \n";
            cout << "5.  trapezoid \n";
            cout << "please select area formula: ";

                switch(yet_another_input){
                case 1:
                cout << "Please enter the base of your triangle and press ENTER: ";
                cin >>b;
                cout << "please enter the hight of your triangle and press ENTER: ";
                cin >> h;
                cout << "the area of your triangle is " << (b*h)/2 << ".";
                goto beginning;

                case 2:
                cout << "Please enter a side length of the square and press ENTER: ";
                cin >> s;
                cout << "the area of your square is " << s*s << ".";
                goto beginning;

                case 3:
                cout << "please enter the base of the rectangle and press ENTER: ";
                cin >> b;
                cout << "please enter the hight of the rectangle and press ENTER: ";
                cin >> h;
                cout << "the area of the rectangle is " << b*h << ".";
                goto beginning;

                case 4:
                cout << "please enter the base of the parallelogram and press ENTER: ";
                cin >> b;
                cout << "please enter the hight of the parallelogram and press ENTER: ";
                cin >> h;
                cout << "the area of the parallelogram is " << b*h << ".";
                goto beginning;

                case 5:
                cout << "please enter the hight of the trapezoid and press ENTER: ";
                cin >> h;
                cout << "please enter the first base and press ENTER: ";
                cin >> base_one;
                cout << "please enter the second base and press ENTER: ";
                cin >> base_two;
                cout << "the area of your trapezoid is " << (base_one+base_two)*(h/2) << ".";
                goto beginning;


                }
            case 2:
            int another_input;
            cout << "**VOLUME** \n";
            cout << "1.  cube \n";
            cout << "2.  rectangular Prism \n";
            cout << "3.  pyramid \n";
            switch (another_input){

                case 1:
                cout << "please enter the side length of your cube and press ENTER: ";
                cin >> s;
                cout << "the volume of your cube is " << s*s*s << ".";
                goto beginning;

                case 2:
                cout << "please enter the length and press ENTER: ";
                cin >> l;
                cout << "please enter the width and press ENTER: ";
                cin >> w;
                cout << "please enter the hight and press ENTER: ";
                cout << "the volume of your rectangular prism is " << l*w*h << ".";
                goto beginning;

                case 3:
                cout << "please enter the base and press ENTER: ";
                cin >> b;
                cout << "please enter the hight and press ENTER: ";
                cin >> h;
                cout << "the volume of your pyramid is " << (b*h)/3 << ".";
                goto beginning;

            }

            }

            case 8:
            goto end;
            }
    end:
    return 0;
    }
The problem I see is you don't have any cin >> another_input; or cin >> yet_another_input; before the switch commands. I think you'll find the program works fine after that.
Thank you very much for your reply. I don't know how I missed that. The only problem now is, once you go to the "area" or "volume" switch, it goes straight to the end of the program.
I think you could try recursive-decent algorithm for your calculator program.
It's much shorter and easier to understand.
You coud refer to "Compiler Construction: Principles and Practice" chapter 2.
Good Luck :)
@else87
Made some changes in your program. Used a do/while loop and put some choices into functions. Hope you like the improvements.
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Calculator 2.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <windows.h>

using namespace std;

void cls(HANDLE hConsole);
void Area();
void Volume();

int input, another_input;
double x, y, b, h, s, base_one, base_two, l, w;
const int rest = 4000; // This gives about 4 seconds before screen clears. Increase/decrease to change delay.

int main(int)
{ 
	HANDLE hStdout;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);  
    do
	{
	cls(hStdout);
	cout << "  ** MAIN MENU ** \n\n";
    cout << "1. Multiplication\n";
    cout << "2. Division\n";
    cout << "3. Addition\n";
    cout << "4. Subtraction\n";
    cout << "5. Square\n";
    cout << "6. Square Root\n";
    cout << "7. Formulas\n\n";
	cout << "8. Quit\n\n";
    cout << "Select choice :\n";
    cin >> input;
	cls(hStdout);
    switch (input)
	{ 
	case 1:
    cout << "Please enter two numbers to be multiplied.";
	cout << "First number : ";
	cin >> x;
	cout << "Now the second number : ";
    cin >> y;
    cout << "The product of " << x << " and " << y << " is " << x*y;
    Sleep(rest);
	break;
	case 2:
    cout << "Please enter two numbers to be divided.\n";
    cout << "First number : ";
	cin >> x;
	cout << "Now the second number : ";
    cin >> y;
    cout << x << " divided by " << y << " is " << x/y;
    Sleep(rest);
	break;
	case 3:
    cout << "Please enter two numbers to be added.\n";
    cout << "First number : ";
	cin >> x;
	cout << "Now the second number : ";
    cin >> y;
    cout << "The sum of " << x << " and " << y << " is " << x+y;
    Sleep(rest);
	break;
    case 4:
    cout << "Please enter two numbers to be subtracted.\n";
    cout << "First number : ";
	cin >> x;
    cout << "Now the second number : ";
	cin >> y;
    cout << "The difference between " << x << " and " << y << " is " << x-y;
    Sleep(rest);
	break;
    case 5:
    cout << "Please enter a number to be squared and press ENTER:\n";
    cin >> x;
    cout << x << " squared is " << x*x << ".";
    Sleep(rest);
	break;
    case 6:
    cout << "Please enter a number to find it's square root and press ENTER:\n";
    cin >> x;
    cout << "the square root of " << x << " is " << sqrt(x) << ".";
    Sleep(rest);
	break;
    case 7:
        cout << "**FORMULAS**\n\n";
        cout << "1. Area\n";
        cout << "2. Volume\n\n";
        cout << "Select formula type: ";
			cin >> another_input;
			cls(hStdout);
			switch(another_input)
			{
            case 1:
             Area();
			 break;
            case 2:
             Volume();
			 break;
            }
	}
  }   while (input!=8);
return 0;
}

void Volume()
{
	int another_input;
	HANDLE hStdout;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	cls(hStdout);
	cout << "**VOLUME**\n\n";
    cout << "1. Cube\n";
    cout << "2. Rectangular Prism\n";
    cout << "3. Pyramid\n\n";
	cout << "Select volume type: ";
		cin >> another_input;
		
            switch (another_input)
				{ 
                case 1:
                cout << "Please enter the side length of your cube and press ENTER: ";
                cin >> s;
                cout << "\nThe volume of your cube is " << s*s*s << ".";
                Sleep(rest);
				break;
                case 2:
                cout << "Please enter the length and press ENTER: ";
                cin >> l;
                cout << "Please enter the width and press ENTER: ";
                cin >> w;
                cout << "Please enter the height and press ENTER: ";
				cin >> h;
                cout << "\nThe volume of your rectangular prism is " << l*w*h << ".";
                Sleep(rest);
				break;

                case 3:
                cout << "Please enter the base and press ENTER: ";
                cin >> b;
                cout << "Please enter the height and press ENTER: ";
                cin >> h;
                cout << "\nThe volume of your pyramid is " << (b*h)/3 << ".";
				Sleep(rest);
	}
} 

void Area()
{
	HANDLE hStdout;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	int yet_another_input;
	cls(hStdout);
            cout << "**AREA**\n\n";
            cout << "1. Triangle\n";
            cout << "2. Square\n";
            cout << "3. Rectangle\n";
            cout << "4. Parallelogram\n";
            cout << "5. Trapezoid\n\n";
            cout << "Please select area formula: ";
				cin >> yet_another_input;
				
                switch(yet_another_input)
		{
                case 1:
                cout << "Please enter the base of your triangle and press ENTER: ";
                cin >> b;
                cout << "Please enter the height of your triangle and press ENTER: ";
                cin >> h;
                cout << "\nThe area of your triangle is " << (b*h)/2 << ".";
                Sleep(rest);
		break;
                case 2:
                cout << "Please enter a side length of the square and press ENTER: ";
                cin >> s;
                cout << "\nThe area of your square is " << s*s << ".";
                Sleep(rest);
		break;
                case 3:
                cout << "Please enter the base of the rectangle and press ENTER: ";
                cin >> b;
                cout << "Please enter the height of the rectangle and press ENTER: ";
                cin >> h;
                cout << "\nThe area of the rectangle is " << b*h << ".";
                Sleep(rest);
		break;
                case 4:
                cout << "Please enter the base of the parallelogram and press ENTER: ";
                cin >> b;
                cout << "Please enter the height of the parallelogram and press ENTER: ";
                cin >> h;
                cout << "\nThe area of the parallelogram is " << b*h << ".";
                Sleep(rest);
		break;
                case 5:
                cout << "Please enter the height of the trapezoid and press ENTER: ";
                cin >> h;
                cout << "Please enter the first base and press ENTER: ";
                cin >> base_one;
                cout << "Please enter the second base and press ENTER: ";
                cin >> base_two;
                cout << "\nThe area of your trapezoid is " << (base_one+base_two)*(h/2) << ".";
                Sleep(rest);
	}
        
}

void cls( HANDLE hConsole)
{
   COORD coordScreen = { 0, 1 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;
// Get the number of character cells in the current buffer. 
   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
   // Fill the entire screen with blanks.
   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }
   // Get the current text attribute.
   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }
   // Set the buffer's attributes accordingly.
   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }
   // Put the cursor at its home coordinates.
   SetConsoleCursorPosition( hConsole, coordScreen );
}
Last edited on
Thank you very much to everyone. It is greatly appreciated.
Even though this post may be solved i need to point 1 small thing:
DO NOT USE GOTO's in C++
Topic archived. No new replies allowed.