how to repeat a menu switch?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clairelee0322
    New Member
    • Jan 2008
    • 13

    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!!



    [CODE=cpp]//Claire's Calculator
    #include <iostream.h>
    #include <math.h>
    double pow(double x, double y);


    main()
    {
    int choice, choicee;
    const double PI = 3.14159;
    float a, b, c;
    int n, m, i;
    double cy;
    double x, y;
    float x_to_the_y;
    double r;
    int re, q;

    do
    {
    cout << "Which calculation you wish to use?" << '\n';
    cout << "1 - Addition" << '\n';
    cout << "2 - Subtraction" << '\n';
    cout << "3 - Multiplication" << '\n';
    cout << "4 - Division " << '\n';
    cout << "5 - Multiplication by a percent " << '\n';
    cout << "6 - Calculate powers" << '\n';
    cout << "7 - Calculate a circle" << '\n';
    cout << "8 - Calculate an exponential" << '\n';
    cin >> choice; //get choice from the user
    if ((choice < 1 ) || (choice > 8))
    {
    cout << "Re-enter your choice.\n";
    }
    }
    while ((choice < 1 ) || (choice > 8));


    switch(choice)
    {
    case 1: // Addition
    cout << "enter 1st number: \n";
    cin >> a;
    cout << "enter 2nd number: \n";
    cin >> b;
    cout << "a + b =" << a + b << '\n';
    break;
    case 2: //Subtraction
    cout << "Enter first number: \n";
    cin >> a;
    cout << "Enter second number: \n";
    cin >> b;
    cout << "a - b = " << a - b << '\n';
    break;
    case 3: //Multiplication
    cout << "Enter first number: \n";
    cin >> a;
    cout << "Enter second number: \n";
    cin >> b;
    cout << "a * b = " << a * b << '\n';
    break;
    case 4: //Division
    do {
    cout << "1 - answer as a decimal\n";
    cout << "2 - answer as a fraction\n";
    cout << "3 - answer as a number with a remainder\n";
    cin >> choicee;
    }
    while ((choicee < 1 ) || (choicee > 3));

    switch(choicee)
    {
    case 1: //answer as a decimal
    cout << "Enter first number: \n";
    cin >> a;
    cout << "Enter second number: \n";
    cin >> b;
    cy = a / b;
    cout << "answer = " << cy << '\n';
    break;
    case 2: //answer as a fraction
    i = 0;
    int Denom, Num;
    cout << "Enter first number: \n";
    cin >> Denom;
    cout << "Enter second number: \n";
    cin >> Num;
    n = Denom;
    m = Num;
    for (i = n * m; i > 1; i--)
    {
    if ((n % i == 0) && (m % i == 0))
    {
    n /= i;
    m /= i;
    cout << "answer=" << n << "/" << m << '\n';
    }
    }
    break;
    case 3: //answer as a number with a remainder
    cout << "Enter first number: \n";
    cin >> a;
    cout << "Enter second number: \n";
    cin >> b;
    q = a / b;
    re = (int)a % (int)b;
    cout << "quotient = " << q << '\n';
    cout << "reminder = " << re << '\n';
    break;
    }
    break;
    case 5: //Multiplication by a percent
    cout << "Enter first number: \n";
    cin >> a;
    cout << "Enter second number:(percent age) \n";
    cin >> b;
    c = a * (b / 100);
    cout << "answer = " << c << '\n';
    break;
    case 6: //Calculate powers
    double pow(double x, double y);
    cout << "Enter first number: \n";
    cin >> x;
    cout << "Enter the power: \n";
    cin >> y;
    x_to_the_y = pow(x, y);
    cout << "answer = " << x_to_the_y << '\n';
    break;
    case 7: //Calculate a circle
    cout << "Please enter r = \n";
    cin >> r;
    cout << "The area is " << PI * r * r << '\n';
    break;
    case 8: // Calculate an exponential
    cout << "Enter first number: \n";
    cin >> a;
    double pow(double x, double y);
    cout << "Enter second number: \n";
    cin >> x;
    cout << "Enter the power: \n";
    cin >> y;
    x_to_the_y = pow(x, y);
    cout << "answer = " << a * x_to_the_y << '\n';
    break;
    }
    return 0;

    }[/CODE]
    Last edited by Ganon11; Jan 23 '08, 07:20 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #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.

    Comment

    • clairelee0322
      New Member
      • Jan 2008
      • 13

      #3
      Originally posted by Ganon11
      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? ??

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #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?

        Comment

        • clairelee0322
          New Member
          • Jan 2008
          • 13

          #5
          Originally posted by Ganon11
          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 ^^

          [code=c;pp]
          #
          break;
          #
          case 8: // Calculate an exponential
          #
          cout << "Enter first number: \n";
          #
          cin >> a;
          #
          double pow(double x, double y);
          #
          cout << "Enter second number: \n";
          #
          cin >> x;
          #
          cout << "Enter the power: \n";
          #
          cin >> y;
          #
          x_to_the_y = pow(x, y);
          #
          cout << "answer = " << a * x_to_the_y << '\n';
          #
          continue;
          #
          }
          #
          return 0;




          If I change all the break; to continue; , the program will continue forever right??




          or the other way? I am not sure althoughI looked up for some resources

          #

          #
          do
          #
          {
          #
          cout << "Which calculation you wish to use?" << '\n';
          #
          cout << "1 - Addition" << '\n';
          #
          cout << "2 - Subtraction" << '\n';
          #
          cout << "3 - Multiplication" << '\n';
          #
          cout << "4 - Division " << '\n';
          #
          cout << "5 - Multiplication by a percent " << '\n';
          #
          cout << "6 - Calculate powers" << '\n';
          #
          cout << "7 - Calculate a circle" << '\n';
          #
          cout << "8 - Calculate an exponential" << '\n';
          #
          cin >> choice; //get choice from the user
          #
          if ((choice < 1 ) || (choice > 8))
          #
          {
          #
          cout << "Re-enter your choice.\n";
          #
          }
          #
          continue;
          }
          #
          while ((choice < 1 ) || (choice > 8));
          [/code]
          Last edited by sicarie; Jan 24 '08, 05:17 AM. Reason: Code tags

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            clairelee0322-

            Please have a look at this, it explains how to use code tags when posting. Thanks

            Comment

            Working...