### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` ``````//***CALCULATOR*** #include #include #include 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248`` ``````// Calculator 2.cpp : Defines the entry point for the console application. #include "stdafx.h" #include #include #include #include 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.