Program With Float Values

This may seem to have a very easy answer, but I'm totally confused. I'm trying to write a program gets two float values from the user. The program should output the equation and answer for the addition, subtraction, multiplication, and division operations.

So you could have something like:

Input: 4 3

Output: 4+3=7 4-3=1 4*3=12 4/3=1.3

I know how to write something that can display the answer from specific variables that I set, but I'm unsure how to write it this way, so it prints those four equations from whichever two numbers the user inputs. Any help you be much appreciated.
use doubles

What every computer scientist should know about floating point arthmetic

http://docs.sun.com/source/806-3568/ncg_goldberg.html
Last edited on
What do you mean by using doubles? Sorry, I'm just completely new at this.
sorry, I edited...

read the above post. A double is a type much like an int.

You're getting the user to input 2 values which seem to be integers. However your output should be a double as there will be decimals involved.
So lets say you have 2 variables input by user and 1 for the answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int num1, num2;
double answer;

cout << "Enter 2 integers";
cin >> num1 >> num2;
answer = num1/num2;
cout << answer << endl;

answer = num1+num2;
cout << answer << endl;
answer = num1*num2;
cout << answer << endl;
// etc 
Last edited on
Thank you so much. You've been a major help. I really appreciate it.
answer = num1/num2;

This actually won't work -- num1/num2 performs integer division (since they're both ints), therefore 'answer' will not be set as you expect.

IE: 5/2 = 2 (not 2.5, even though answer is a double)

You'd need to cast num1 or num2 (or both) to double before the division:

 
answer = double(num1) / num2;
Topic archived. No new replies allowed.