# Trying to make a simple calculator

Page 1 of 1

## 6 Replies - 1066 Views - Last Post: 30 June 2011 - 07:06 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=237599&amp;s=a1d678a2c6ce528a6e69c69f6ab43e9d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 IsoChronous

Reputation: 0
• Posts: 9
• Joined: 29-June 11

# Trying to make a simple calculator

Posted 30 June 2011 - 05:52 AM

Im just starting C++ and Im trying to make a simple calculator program (no GUI yet, just command line). My question has three parts: first, I cannot seems to get it to do anything other than add two numbers. it skips right over the "subtract", "multiply", and "divide" statements. I am unable to see what i did wrong. second question: how to use the "float" variable? I read that using "float" will allow for decimals (important in a calculator). Last question: how do i allow for an infinite amount of numbers to be added, divided, etc? i mean if i wanted to multiply 17 different numbers, how would that look in code? heres my code so far.
```#include <iostream>
#include <string>
using namespace std;
int mult (int x, int y);
int main()
{
int x;
int y;
cout << "Give me two numbers: ";
cin >> x >> y;
cin.ignore();
cout << "Do you want to add, multiply, subtract, or divide?\n";
cin.ignore();
{
cout << "These two numbers equal "<< x+y <<".\n";
}
int mult (int x, int y);
{
return x+y;
}
{
cout << "These two numbers equal "<< x*y <<".\n";
}
int mult (int x, int y);
{
return x*y;
}
{
cout << "These two numbers equal "<< x-y <<".\n";
}
int mult (int x, int y);
{
return x-y;
}
{
cout << "These two numbers equal "<< x/y <<".\n";
}
int mult (int x, int y);
{
return x/y;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Trying to make a simple calculator

### #2 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 06:27 AM

You keep returning the answer to main, everytime you call mult it returns the answer to main.

Why do you have the same mult function defined over and over? You don't even need those functions, you are already doing the arithmetic when you output the answer.

### #3 IsoChronous

Reputation: 0
• Posts: 9
• Joined: 29-June 11

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:03 AM

Well like I said, I'm still learning. So, removing those "int mult" lines should fix the issue?

### #4 brandon_v

Reputation: 2
• Posts: 62
• Joined: 29-May 08

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:22 AM

Yea, remove those "int mult" lines, including the {.blah.blah.blah.} from there.
What you're doing is defining the mult function several times, and inside another function, both of which programming do nots.

### #5 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 08:13 AM

I would also recommend that you get the user to choose a number rather than doing a string comparison, just say choose 1 for addition, 2 for multiplying etc..

### #6 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 10:16 AM

An example of using a switch, as stayCrisp suggested

```#include <iostream>
#include <string>

using namespace std;

int main()
{
int choice, x, y;
cout << "Give me two numbers: ";
cin >> x >> y;
cout << "Do you want to (1) add, (2) multiply, (3) subtract, or (4) divide?\n";
cin >> choice;

switch(choice) {
case 1 :
break;
case 2:
//code to multiply numbers
break;
case 3:
//code to subract numbers
break;
case 4:
//code to divide numbers
break;
default:
//code to handle if user entered a number that is not 1-4
}
return 0;
}

```

### #7 dragon40226

Reputation: 0
• Posts: 34
• Joined: 29-June 11

## Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:06 PM

I wrote a simple calc like this a couple days ago here is my code:
```#include <iostream>
#include <cstdlib>

using namespace std;

int main () {
char choice;
double num;
double num2;

for (;;)/>{
do {
cout << "What function would you like to do?\n";
cout << "There are 4 functions.\n";
cout << "1 - For Additon.\n";
cout << "2 - For Subtraction.\n";
cout << "3 - For Division.\n";
cout << "4 - For Multiplication.\n";
cout << "Made By Andrew Corp :D/> \n";
cout << "Enter which one you would like to do here: \n";
cin >> choice;
} while ( choice < '1' || choice > '4' && choice != 'q');
if (choice == 'q') break;
switch (choice) {

case '1':
cout << "Enter the first number to add: ";
cin >> num;
cout << "Enter the second number to add: ";
cin >> num2;
cout << "The sum of the two is: " << num + num2 << endl;
cout << " ";
cout << " ";
break;

case '2':
cout << "Enter the first number to subtract: ";
cin >> num;
cout << "Enter the second number to subtract: ";
cin >> num2;
cout << "The difference of the two is: " << num - num2 << endl;
cout << " ";
cout << " ";
break;

case '3':
cout << "Enter the dividend: ";
cin >> num;
cout << "Enter the divisor: ";
cin >> num2;
cout << "The quotient is: " << num / num2 << endl;
cout << " ";
cout << " ";
break;

case '4':
cout << "Enter the first number to multiply: ";
cin >> num;
cout << "Enter the second number to multiply: ";
cin >> num2;
cout << "The product is: " << num * num2 << endl;
cout << " ";
cout << " ";
break;

default:
cout<<"That is not an option, Nice Try Though!";
}
}
return 0;
}

```