Connecting Tech Pros Worldwide Help | Site Map

Calculator problems-

Newbie
 
Join Date: Jan 2008
Location: California
Posts: 13
#1: Jan 17 '08
This program doesn't run because some problem with the reduce fraction.
My reduce fraction is all right but when I tried to put that in this program, It has some errors.
Or maybe the switch menu is wrong? because I put another swith menu in the main switch menu.

I guess the problem is In the division - fraction section
the error says something like cases are skipped by something.. I don't remember because I only get the chances to use C++ in the class

Thanks guys! I appreciate it.


1) Addition
2) Subtraction
3) Multiplication
4) Division
a) answer as a decimal
b) answer as a fraction
c) answer as a number with a remainder
5) Multiplication by a percent (i.e. 10 times 20% equals 2)
6) Calculate powers. (i.e. 23=8)
7) Be able to figure out the area of a circle because you have PI=3.14159 on your calculator
8) Calculate an exponential (i.e. 8 x 103 = 8e3)








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;
  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.             int n, m, i = 0;
  82.             int Denom, Num;
  83.         cout << "Enter first number: \n";
  84.         cin >> n;
  85.         cout << "Enter second number: \n";
  86.         cin >> m;
  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.                }
  96.        }
  97.        cout << "a=" << n << "/" << m << '\n';
  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.     }
  109.  
  110.     case 5:    //Multiplication by a percent
  111.         cout << "Enter first number: \n";
  112.         cin >> a;
  113.         cout << "Enter second number:(percentage) \n";
  114.         cin >> b;
  115.         c = a * (b / 100);
  116.         cout << "answer = " << c << '\n';
  117.         break;
  118.     case 6:    //Calculate powers
  119.         double pow(double x, double y);
  120.         cout << "Enter first number: \n";
  121.         cin >> x;
  122.         cout << "Enter the power: \n";
  123.         cin >> y;    
  124.         x_to_the_y = pow(x, y);
  125.         cout << "answer = " << x_to_the_y << '\n';
  126.         break;    
  127.     case 7:   //Calculate a circle
  128.         cout << "Please enter r = \n";
  129.         cin >> r;
  130.         cout << "The area is " << PI * r * r << '\n';
  131.         break;
  132.     case 8:    // Calculate an exponential
  133.         cout << "Enter first number: \n";
  134.         cin >> a;
  135.         double pow(double x, double y);
  136.         cout << "Enter second number: \n";
  137.         cin >> x;
  138.         cout << "Enter the power: \n";
  139.         cin >> y;    
  140.         x_to_the_y = pow(x, y);
  141.         cout << "answer = " << a * x_to_the_y << '\n';
  142.         break;
  143.     }
  144.     return 0;
  145. }
Member
 
Join Date: Sep 2007
Location: New Delhi
Posts: 33
#2: Jan 18 '08

re: Calculator problems-


Hi ,

I researched on your Calculate program. The program is having a few errors. Please have a look at the following points:-
1. A break statement is necessary before case 5. i.e., just after the nested switch finishes.
2. There is no means to exit the program. I mean you need to assign/choose some specific values of variables choice and chicee also which would exit from the code.
3. You have declared the function double pow(double x, double y); so many times which is unneccasary because you are not defining this function. Instead you are using the standard libray function pow().
4. The variables m,n declared global as well as local too in case 4. Some of these are unnecessary.

The problem is nothing but its only due to the unnecessary complexity you are generating in your code. Try to be simple and remove extra code.

If you have any questions, feel free to ask.

Regards,
Raj Kumar Arora
Reply