473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to repeat a menu switch?

Here's my program, the only problem is that I don't know how to display the menu again after the user used the calcultor. I know it has do with DO WHILE LOOP but I don't know how to put it.. please help me out! Thanks!!



Expand|Select|Wrap|Line Numbers
  1. //Claire's Calculator
  2. #include <iostream.h>
  3. #include <math.h>
  4. double pow(double x, double y);
  5.  
  6.  
  7. main()
  8. {
  9.     int choice, choicee;
  10.     const double PI = 3.14159;
  11.     float a, b, c;
  12.     int n, m, i;
  13.     double cy;
  14.     double x, y;
  15.     float x_to_the_y;
  16.     double r;
  17.     int re, q;
  18.  
  19.     do 
  20.     {
  21.         cout << "Which calculation you wish to use?" << '\n';
  22.         cout << "1 - Addition" << '\n';
  23.         cout << "2 - Subtraction" << '\n';
  24.         cout << "3 - Multiplication" << '\n';
  25.         cout << "4 - Division " << '\n';
  26.         cout << "5 - Multiplication by a percent " << '\n';
  27.         cout << "6 - Calculate powers" << '\n';
  28.         cout << "7 - Calculate a circle" << '\n';
  29.         cout << "8 - Calculate an exponential" << '\n';
  30.         cin >> choice;    //get choice from the user
  31.         if ((choice < 1 ) || (choice > 8))
  32.         {
  33.             cout << "Re-enter your choice.\n";
  34.         }
  35.     }    
  36.     while ((choice < 1 ) || (choice > 8));
  37.  
  38.     switch(choice)
  39.     {
  40.     case 1:   // Addition
  41.         cout << "enter 1st number: \n";
  42.         cin >> a;
  43.         cout << "enter 2nd number: \n";
  44.         cin >> b;
  45.         cout << "a + b =" << a + b << '\n';
  46.         break;
  47.     case 2:    //Subtraction
  48.         cout << "Enter first number: \n";
  49.         cin >> a;
  50.         cout << "Enter second number: \n";
  51.         cin >> b;
  52.         cout << "a - b = " << a - b << '\n';
  53.         break;
  54.     case 3:      //Multiplication
  55.         cout << "Enter first number: \n";
  56.         cin >> a;
  57.         cout << "Enter second number: \n";
  58.         cin >> b;
  59.         cout << "a * b = " << a * b << '\n';
  60.         break;
  61.     case 4:    //Division
  62.         do {
  63.             cout << "1 - answer as a decimal\n"; 
  64.             cout << "2 - answer as a fraction\n";
  65.             cout << "3 - answer as a number with a remainder\n";
  66.             cin >> choicee;
  67.         }        
  68.     while ((choicee < 1 ) || (choicee > 3));
  69.  
  70.     switch(choicee)
  71. {
  72.     case 1:  //answer as a decimal
  73.         cout << "Enter first number: \n";
  74.         cin >> a;
  75.         cout << "Enter second number: \n";
  76.         cin >> b;
  77.         cy = a / b;
  78.         cout << "answer = " << cy << '\n';
  79.         break;
  80.     case 2: //answer as a fraction
  81.         i = 0;
  82.         int Denom, Num;
  83.         cout << "Enter first number: \n";
  84.         cin >> Denom;
  85.         cout << "Enter second number: \n";
  86.         cin >> Num;
  87.         n = Denom;
  88.         m = Num;
  89.         for (i = n * m; i > 1; i--)
  90.         {
  91.             if ((n % i == 0) && (m % i == 0))
  92.             {
  93.                 n /= i;
  94.                 m /= i;
  95.             cout << "answer=" << n << "/" << m << '\n';
  96.             }
  97.         }
  98.     break;
  99.     case 3: //answer as a number with a remainder
  100.         cout << "Enter first number: \n";
  101.         cin >> a;
  102.         cout << "Enter second number: \n";
  103.         cin >> b;
  104.         q = a / b;
  105.         re = (int)a % (int)b;
  106.         cout << "quotient = " << q << '\n';
  107.         cout << "reminder = " << re << '\n';
  108.         break;
  109.     }
  110.     break;
  111.     case 5:    //Multiplication by a percent
  112.         cout << "Enter first number: \n";
  113.         cin >> a;
  114.         cout << "Enter second number:(percentage) \n";
  115.         cin >> b;
  116.         c = a * (b / 100);
  117.         cout << "answer = " << c << '\n';
  118.         break;
  119.     case 6:    //Calculate powers
  120.         double pow(double x, double y);
  121.         cout << "Enter first number: \n";
  122.         cin >> x;
  123.         cout << "Enter the power: \n";
  124.         cin >> y;    
  125.         x_to_the_y = pow(x, y);
  126.         cout << "answer = " << x_to_the_y << '\n';
  127.         break;    
  128.     case 7:   //Calculate a circle
  129.         cout << "Please enter r = \n";
  130.         cin >> r;
  131.         cout << "The area is " << PI * r * r << '\n';
  132.         break;
  133.     case 8:    // Calculate an exponential
  134.         cout << "Enter first number: \n";
  135.         cin >> a;
  136.         double pow(double x, double y);
  137.         cout << "Enter second number: \n";
  138.         cin >> x;
  139.         cout << "Enter the power: \n";
  140.         cin >> y;    
  141.         x_to_the_y = pow(x, y);
  142.         cout << "answer = " << a * x_to_the_y << '\n';
  143.         break;
  144.     }
  145.     return 0;
  146.  
  147. }
Jan 23 '08 #1
5 10890
Ganon11
3,652 Expert 2GB
You are correct, it has to do with a do...while loop. What will be the end condition? What do you want to loop? Answer these questions and you will be 95% of the way to your answer.
Jan 23 '08 #2
You are correct, it has to do with a do...while loop. What will be the end condition? What do you want to loop? Answer these questions and you will be 95% of the way to your answer.

mm.. I want it to display its menu again after the user used the calculator once.....The end condition? what do you mean? When the case ends? ??
Jan 23 '08 #3
Ganon11
3,652 Expert 2GB
Exactly. When do you want this loop to stop e.g. the menu to stop displaying? Do you want it stop when the user enters a certain character (N)? Do you want it to stop when the result they got is larger than some number? Do you want it to randomly stop?
Jan 24 '08 #4
Exactly. When do you want this loop to stop e.g. the menu to stop displaying? Do you want it stop when the user enters a certain character (N)? Do you want it to stop when the result they got is larger than some number? Do you want it to randomly stop?
Thanks alot! Ganon11!! :D ^^

Expand|Select|Wrap|Line Numbers
  1. #
  2.         break;
  3. #
  4.     case 8:    // Calculate an exponential
  5. #
  6.         cout << "Enter first number: \n";
  7. #
  8.         cin >> a;
  9. #
  10.         double pow(double x, double y);
  11. #
  12.         cout << "Enter second number: \n";
  13. #
  14.         cin >> x;
  15. #
  16.         cout << "Enter the power: \n";
  17. #
  18.         cin >> y;   
  19. #
  20.         x_to_the_y = pow(x, y);
  21. #
  22.         cout << "answer = " << a * x_to_the_y << '\n';
  23. #
  24.         continue;
  25. #
  26.     }
  27. #
  28.     return 0;
  29.  
  30.  
  31.  
  32.  
  33. If I change all the break; to continue; , the program will continue forever right??
  34.  
  35.  
  36.  
  37.  
  38. or the other way? I am not sure althoughI looked up for some resources
  39.  
  40. #
  41.  
  42. #
  43.     do
  44. #
  45.     {
  46. #
  47.         cout << "Which calculation you wish to use?" << '\n';
  48. #
  49.         cout << "1 - Addition" << '\n';
  50. #
  51.         cout << "2 - Subtraction" << '\n';
  52. #
  53.         cout << "3 - Multiplication" << '\n';
  54. #
  55.         cout << "4 - Division " << '\n';
  56. #
  57.         cout << "5 - Multiplication by a percent " << '\n';
  58. #
  59.         cout << "6 - Calculate powers" << '\n';
  60. #
  61.         cout << "7 - Calculate a circle" << '\n';
  62. #
  63.         cout << "8 - Calculate an exponential" << '\n';
  64. #
  65.         cin >> choice;    //get choice from the user
  66. #
  67.         if ((choice < 1 ) || (choice > 8))
  68. #
  69.         {
  70. #
  71.             cout << "Re-enter your choice.\n";
  72. #
  73.         }
  74. #
  75. continue;
  76.     }   
  77. #
  78.     while ((choice < 1 ) || (choice > 8));
Jan 24 '08 #5
sicarie
4,677 Expert Mod 4TB
clairelee0322-

Please have a look at this, it explains how to use code tags when posting. Thanks
Jan 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark Preston | last post by:
Its perhaps a bit early to ask, since I'm still doing the page design and haven't got down to the coding yet, but I wonder if anyone can help with a bit of a question. To lay the groundwork, a...
5
by: Andy | last post by:
Hi I am writing a small calculator program using switch() for a menu. The menu is presented at first, selection read in via scanf(), and calculation is executed. At the end of the operation, the...
3
by: prakashwadhwani | last post by:
When my application starts 2 toolbars still remain open 1) Form View and 2) Formatting (Form/Report) Toolbar Also ... Access's default Menu-Bar also remains open. How to switch it off at...
3
by: John | last post by:
Hi there, I was reading an article (http://avenuea-razorfish.com/articles/TheAll-MenuNavigation_Turbek.pdf) on 'all-menu navigation' and I'd like to try and implement this in my site. Can...
5
by: Brad Isaacs | last post by:
Good morning friends, I am working with ASP.NET 2.0 -- VB code behind I have created tabbed pages using the Menu control with the Multiview control. Using the menu control to display the...
7
by: Jon Slaughter | last post by:
Can someone point me to some resources on how to tile an image to create a border? <td> <img src="images/MTile.png" width="1" height="30" alt=""></td> I have something like that where I need...
3
by: ApexData | last post by:
Access2k non runtime. In a previous post, I mentioned that I wanted to hide the Access 'Menu Bar' from view. That means that I DO NOT want to be able to see the MenuBar, even as a momentary...
2
XedinUnknown
by: XedinUnknown | last post by:
Hi! I am new to this forum, but not new to web design and programming. Nevertheless, I have never tried to use PNG so extensively in my pages. here's the problem. First, I have found that the...
4
by: N00b13 | last post by:
I have a great JS menu but I have to update every page each time I want to change a link. Is there a way to store my links in a file and call it so i only change that file? (what I have tried so far...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.