473,320 Members | 1,817 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,320 software developers and data experts.

Calculator problems-

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. }
Jan 17 '08 #1
1 2521
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
Jan 18 '08 #2

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

Similar topics

6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
1
by: defcon8 | last post by:
Hello. I have been trying to use PIL's .putdata() method and I have been having some problems. The error I get is: Traceback (most recent call last): File "realautomata.py", line 72, in ?...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.