# simple calculator program that adds, subtracts, multiplies and divides

Page 1 of 1

## 5 Replies - 10983 Views - Last Post: 27 October 2008 - 12:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=69162&amp;s=4524011ba8ba42ade012eef2683d27a0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

# simple calculator program that adds, subtracts, multiplies and divides

Posted 26 October 2008 - 04:23 PM

Write a simple calculator program that adds, subtracts, multiplies and divides. When
the program is run, it initiliazes the result to 0. The user can then type in an operator
and number. The result is updated and displayed. The following operators are valid:

Operator Meaning
--------------------------------
- Subtraction
* Multiplication
/ Division
---------------------------------

Handle the case when the user enters q or Q to quit and h or H for usage help.

Problem: when the program runs and the user enter an operator, it goes on and on and on.

```#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{
int math;
int number;
int result = 0;
char H,h, Q, q;

cout << " Result: " << result;

cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
cin  >> math;
cout << endl;

cout << "Enter the value: ";
cin  >> number;
cout << endl;

while (math != Q || math != q) {
cout << endl;
cout << setw(10) << "Operator" << setw(15) << "Meaning" << endl;
cout << setw(10) << "  ----------------------------" << endl;
cout << setw(5) << "+" << setw(20) << "Addition" << endl;
cout << setw(5) << "-" << setw(20) << "Subtraction" << endl;
cout << setw(5) << "*" << setw(20) << "Multiplication" << endl;
cout << setw(5) << "/" << setw(20) << "Division" << endl;

cout << "Enter the value: ";
cin  >> number;
cout << endl;

cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
cin  >> math;
cout << endl;

cout << " Result: " << result;

cout << endl;
cout << endl;

switch (math) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
if (number==0);
cout <<  "Division by zero";
cout << endl;
break;
case 'H':
case 'h':

cout << "Use '+' to add a value to the result" << endl;
cout << "Use '-' to subtract a value from the result" << endl;
cout << "Use '*' to multiply a value with the result" << endl;
cout << "Use '/' to divide a value into the result" << endl;
break;

case 'Q':
case 'q':
cout << "Thank you for using our calculator. Come back again :-)." << endl;
break;

system ("pause");

}
}

system ("pause");
return 0;

}

```

Is This A Good Question/Topic? 0

## Replies To: simple calculator program that adds, subtracts, multiplies and divides

### #2 BenAshton24

Reputation: 2
• Posts: 18
• Joined: 26-October 08

## Re: simple calculator program that adds, subtracts, multiplies and divides

Posted 26 October 2008 - 06:33 PM

Hey, is this homework??? hmm... looks like an assignment

First of all, I changed your integers to chars so that you don't get the random looping, then for some reason your code seems a bit odd... you have got stuff all over the place, seemingly for no apparent reason :S not sure if i read your question incorrectly but i have rewritten it so that it looks and functions in the way that i understand you want it to. Hope this helps, Ben.

```#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char math,number,H,h, Q, q,result = 0;
cout << " Result: " << result;
cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
while (math != Q || math != q) {
cout << endl;
cout << setw(10) << "Operator" << setw(15) << "Meaning" << endl;
cout << setw(10) << "  ----------------------------" << endl;
cout << setw(5) << "+" << setw(20) << "Addition" << endl;
cout << setw(5) << "-" << setw(20) << "Subtraction" << endl;
cout << setw(5) << "*" << setw(20) << "Multiplication" << endl;
cout << setw(5) << "/" << setw(20) << "Division" << endl;
cin  >> math;
cout << endl;
cout << "Enter the value: ";
cin  >> number;
cout << endl;
switch (math) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
if (number==0);
cout <<  "Division by zero";
cout << endl;
break;
case 'H':
case 'h':
cout << "Use '+' to add a value to the result" << endl;
cout << "Use '-' to subtract a value from the result" << endl;
cout << "Use '*' to multiply a value with the result" << endl;
cout << "Use '/' to divide a value into the result" << endl;
break;
case 'Q':
case 'q':
cout << "Thank you for using our calculator. Come back again :-)." << endl;
break;
}

cout << " Result: " << result << endl;
system("pause");
}
system ("pause");
return 0;
}
```

This post has been edited by BenAshton24: 26 October 2008 - 06:42 PM

### #3 Soura

Reputation: 0
• Posts: 27
• Joined: 24-September 08

## Re: simple calculator program that adds, subtracts, multiplies and divides

Posted 27 October 2008 - 01:23 AM

qwert12345, on 26 Oct, 2008 - 04:23 PM, said:

Write a simple calculator program that adds, subtracts, multiplies and divides. When
the program is run, it initiliazes the result to 0. The user can then type in an operator
and number. The result is updated and displayed. The following operators are valid:

Operator Meaning
--------------------------------
- Subtraction
* Multiplication
/ Division
-------------------------

The Program is-
```main()
{
float a, b, r;
int n;
printf ("MENU: \n Press '1' For ''Addition'' \n Press '2' For ''Substraction''\n Press '3' For ''Multiplication''\n Press '4' For ''divition'' \n ");
scanf("%d", &n);
if(n==1 || n==2 || n==3 || n==4)
{
printf(" Enter the first no. ");
scanf("%f", &a);
printf(" Enter the second no. ");
scanf("%f", &b);
switch(n)
{
case 1:
r=a+b;
break;
case 2:
r=a-b;
break;
case 3:
r=a*b;
break;
case 4:
r=a/b;
}
printf (" The Result is = %f", r);

}
else
{
printf ("Your CHOICE is WRONG, Please Enter the RIGHT CHOICE.\n Press any key to Exit.");
}

getch();
}
```

### #4 lnc12

• D.I.C Regular

Reputation: 1
• Posts: 268
• Joined: 21-May 08

## Re: simple calculator program that adds, subtracts, multiplies and divides

Posted 27 October 2008 - 02:23 AM

Thank you I need a basic calculator, to make another program.

### #5 Commodore

Reputation: 0
• Posts: 7
• Joined: 27-October 08

## Re: simple calculator program that adds, subtracts, multiplies and divides

Posted 27 October 2008 - 09:09 AM

Finally!!
A question i can give an repy to hehe.

```# include <stdio.h>

int main (void) {

int i;
int b;
int a;
int c;

printf("Press 1 to addition\nPress 2 for substraction\nPress 3 for multiplication\nPress 4 for division\n");
scanf("%d",&i);
scanf("%d",&b);
scanf("%d",&a);

if (i == 1)
c = b + a;

if (i == 2)
c = b - a;
if (i == 3)
c =b * a;
if (i == 4)
c= b/a;

printf("The correct andswer is %d\n",c);

return 0;
}
```

it is very primitive, but i only use c++ for a month or 2

### #6 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

## Re: simple calculator program that adds, subtracts, multiplies and divides

Posted 27 October 2008 - 12:41 PM

BenAshton24, on 26 Oct, 2008 - 06:33 PM, said:

Hey, is this homework??? hmm... looks like an assignment

First of all, I changed your integers to chars so that you don't get the random looping, then for some reason your code seems a bit odd... you have got stuff all over the place, seemingly for no apparent reason :S not sure if i read your question incorrectly but i have rewritten it so that it looks and functions in the way that i understand you want it to. Hope this helps, Ben.

```#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char math,number,H,h, Q, q,result = 0;
cout << " Result: " << result;
cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
while (math != Q || math != q) {
cout << endl;
cout << setw(10) << "Operator" << setw(15) << "Meaning" << endl;
cout << setw(10) << "  ----------------------------" << endl;
cout << setw(5) << "+" << setw(20) << "Addition" << endl;
cout << setw(5) << "-" << setw(20) << "Subtraction" << endl;
cout << setw(5) << "*" << setw(20) << "Multiplication" << endl;
cout << setw(5) << "/" << setw(20) << "Division" << endl;
cin  >> math;
cout << endl;
cout << "Enter the value: ";
cin  >> number;
cout << endl;
switch (math) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
if (number==0);
cout <<  "Division by zero";
cout << endl;
break;
case 'H':
case 'h':
cout << "Use '+' to add a value to the result" << endl;
cout << "Use '-' to subtract a value from the result" << endl;
cout << "Use '*' to multiply a value with the result" << endl;
cout << "Use '/' to divide a value into the result" << endl;
break;
case 'Q':
case 'q':
cout << "Thank you for using our calculator. Come back again :-)." << endl;
break;
}

cout << " Result: " << result << endl;
system("pause");
}
system ("pause");
return 0;
}
```

Hi Ben, Thanks for helping me. Your program doesn't allow me to input any Operators : like *, +, -, / . and to the result.

this is the expected output for my homework:
Example output:

Result: 0
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide
+
Enter the value : 23
0 + 23
Result: 23
Enter operator from the table below, Q or q for quit, H or h for help :

/
Enter the value : 0
Error:Divide by zero. Operation ignored
Result: 23
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide
*
Enter the value : 3
23 * 3
Result: 69
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide
-
Enter the value : 56
69 - 56
Result: 13
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide
/
Enter the value : 4
13 / 4
Result: 3.25
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide

u

Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======
- Subtract
* Multiply
/ Divide
h
Use '+' to add a value to the result ( 3.25 ),
Use '-' to subtract a value from the result ( 3.25 ),
Use '*' to multiply a value with the result ( 3.25 ),
Use '\' to divide a value into the result ( 3.25 ),
Result: 3.25
Enter operator from the table below, Q or q for quit, H or h for help :

Operator Meaning
======== =======