sicotic87

Basic calc do-while main menu loop error

Aug 7th, 2012
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. // Basic calculator MISSING MAIN MENU LOOP
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.     //Main Menu Loop Attempt
  13.    
  14.     do
  15.     {
  16.  
  17.     int choice;
  18.  
  19.     //Main Menu
  20.     cout << "Enter the number next to the calculation you would like to use, or \n if you would like to exit." << endl;
  21.     cout << endl;
  22.     cout << endl;
  23.     cout << "1. Addition" << endl;
  24.     cout << "2. Subtraction" << endl;
  25.     cout << "3. Multiplication" << endl;
  26.     cout << "4. Division" << endl;
  27.     cout << "5. Exit" << endl;
  28.     cout << endl;
  29.     cout << endl;
  30.     cin >> choice;
  31.  
  32.    
  33.    
  34.     //Addition statement
  35.             if (choice == 1)
  36.     {
  37.         double firstNumber;
  38.         double secondNumber;
  39.         double result;
  40.         char returnToMainMenu;
  41.  
  42.  
  43.             cout << "You chose addition." << endl;
  44.             cout << endl;
  45.             cout << "Enter the first number: ";
  46.             cin >> firstNumber;
  47.             cout << endl;
  48.             cout << "Now, enter the second number to add: ";
  49.             cin >> secondNumber;
  50.             cout << endl;
  51.            
  52.             result = firstNumber + secondNumber;
  53.  
  54.             cout << firstNumber << " + " << secondNumber << " = " << result << endl;
  55.             cout << endl;
  56.             cout << endl;
  57.             cout << "Would you like to return to the main menu? (y,n):";
  58.             cin >> returnToMainMenu;
  59.             //cout << "Press any key to exit.";
  60.            
  61.             //getch();
  62.             return 0;
  63.     }
  64.    
  65.             //Subtraction statement
  66.     else if (choice == 2)
  67.     {
  68.         double firstNumber;
  69.         double secondNumber;
  70.         double result;
  71.         char returnToMainMenu;
  72.  
  73.         cout << "You chose subtraction." << endl;
  74.         cout << endl;
  75.         cout << "Enter the first number: ";
  76.         cin >> firstNumber;
  77.         cout << endl;
  78.         cout << "Enter the second number to subtract: ";
  79.         cin >> secondNumber;
  80.         cout << endl;
  81.  
  82.         result = firstNumber - secondNumber;
  83.  
  84.         cout << firstNumber << " - " << secondNumber << " = " << result << endl;
  85.         cout << endl;
  86.         cout << endl;
  87.         cout << "Would you like to return to the main menu? (y,n):";
  88.         cin >> returnToMainMenu;
  89.         //cout << "Press any key to exit.";
  90.         //cin.get();
  91.         return 0;
  92.     }
  93.             //Multiplication statement
  94.     else if (choice == 3)
  95.     {
  96.  
  97.         double firstNumber;
  98.         double secondNumber;
  99.         double result;
  100.         char returnToMainMenu;
  101.  
  102.         cout << "You chose multiplication." << endl;
  103.         cout << endl;
  104.         cout << "Enter the first number: ";
  105.         cin >> firstNumber;
  106.         cout << endl;
  107.         cout << "Enter the second number to multiply: ";
  108.         cin >> secondNumber;
  109.         cout << endl;
  110.        
  111.         result = firstNumber * secondNumber;
  112.  
  113.         cout << firstNumber << " * " << secondNumber << " = " << result << endl;
  114.         cout << endl;
  115.         cout << endl;
  116.         cout << "Would you like to return to the main menu?(y,n):";
  117.         cin >> returnToMainMenu;
  118.         //cout << "Press any key to exit.";
  119.         //cin.get();
  120.         return 0;
  121.     }
  122.     //Division statement
  123.     else if (choice == 4)
  124.     {
  125.         double firstNumber;
  126.         double secondNumber;
  127.         double result;
  128.         char returnToMainMenu;
  129.  
  130.         cout << "You chose division." << endl;
  131.         cout << endl;
  132.         cout << "Enter the first number: ";
  133.         cin >> firstNumber;
  134.         cout << endl;
  135.         cout << "Enter the second number to divide: ";
  136.         cin >> secondNumber;
  137.         cout << endl;
  138.        
  139.         result = firstNumber / secondNumber;
  140.  
  141.         cout << firstNumber << " / " << secondNumber << " = " << result << endl;
  142.         cout << endl;
  143.         cout << endl;
  144.         cout << "Would you like to return to the main menu?(y,n):";
  145.         cin >> returnToMainMenu;
  146.         //cout << "Press any key to exit.";
  147.         //cin.get();
  148.         return 0;
  149.     }
  150.     //Exit statement
  151.     else if (choice == 5)
  152.     {
  153.         return 0;
  154.     }
  155.     //Invalid choice loop
  156.         do
  157.     {
  158.        
  159.         cout << "You entered an invalid choice." << endl;
  160.         cout << "Please choose one of the following:" << endl;
  161.         cout << endl;
  162.         cout << endl;
  163.         cout << "1. Addition" << endl;
  164.         cout << "2. Subtraction" << endl;
  165.         cout << "3. Multiplication" << endl;
  166.         cout << "4. Division" << endl;
  167.         cout << "5. Exit" << endl;
  168.         cout << endl;
  169.         cin >> choice;
  170.     }
  171.     while (choice < 1 || choice > 5);
  172.     }
  173.     while (returnToMainMenu == 'y');
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment