# I've compiled, but I don't know what the problem is.

• 01-22-2002
iluvmyafboys
I've compiled, but I don't know what the problem is.
I haven't had any problems with my other programming, but this one seems to stump me. The program asks to "write a program that asks the user to enter two numbers, obtains the two numbers and prints the sum, product, difference, and quotient of the two numbers." I understand write it if it asked for the "If" statements, but I'm not sure how to program all four functions in the same file. Here is what I have. Any help will be greatly appreciated!

// Ex. 1.23: ex123.cpp
// Sum, difference, product, and quotient program
#include <iostream>

int main()
{
int integer1, integer2, sum; // declaration
integer1, integer2, difference;
integer1, integer2, product;
integer1, integer2, quotient;

std::cout << "Enter first integer\n"; // prompt
std::cin >> integer1; // read an integer
std::cout << "Enter second integer\n"; // prompt
std::cin >> integer2; // read an integer
sum = integer1 + integer2; // assignment of sum
std::cout << "Sum is " << sum << std::endl; // print sum
difference = integer1 - integer2; // assignment of difference
std::cout << "Difference is " << difference << std::endl;
product = integer1 * integer2; // assignment of product
std::cout << "Product is " << product << std::endl;
quotient = integer1 / integer2; // assignment of quotient
std::cout << "Quotient is " << quotient << std::endl;

return 0; // indicate that program ended successfully
}
• 01-22-2002
matheo917
Inside the main() function, the second, third, and fourth line are not declared....

What i mean by this is: you should not include integer1 and integer2 any more b/c they were declared already (on the first line)...

All you need to declare are Poduct, Difference, Quotient....

i.e:

float Product; // just replace your 2nd, 3rd, 4th lines with
float Difference; // those .....
float Quotient;

...........

the reason i declared them as "float" is b/c if u divide 2 integers you might get a fraction, so in this way they can be expressed as a fractional number....

when i compiled it everything was o.k.

Good Luck

ps. if you are running this under Windows you will need some kind of a stopper before your last "return 0" statement so you can see the output, otherwise the the console will disapear before you can check your output..............unless you are running this under DOS then this won't be a problem

Regards,
matheo917
• 01-22-2002
iluvmyafboys
I'm still having a problem. I changed each integer statement to the floats, and it runs, but now the output says:

Enter first integer
5
Enter second integer
8
Sum is 13
Difference is -3
Product is 40
Quotient is 0

Any idea as to why the quotient doesn't come out as a fraction/decimal?
• 01-22-2002
Leeman_s
yes
You don't only need the variable quotient to be of type float or double, but you also need the two numbers the user enters (integer1, integer2) to be of type float or double. That'll fix it.
• 01-22-2002
matheo917
yes, there is a explanation to this....

the reason behind all this is something called "casting", or simply in this case changing from (for example) INTEGER TO FLOAT...

when you have your line of code :

quotien = integer1/integer2;

it doesn't exactly work as in math.... what the compiler does is this:::: [ takes INT integer1 and divides it by INT integer2] now the value that comes back from this is a INTEGER (not a float).... after the compiler receives that value (INT) it assigns it to "quotient", wich in this case is a FLOAT, simply said CANNOT ASSIGN INTEGER TO A FLOAT...

What you need to do is "cast" those two integers into Floats before the division accurs so that two floats can be divided instead of two integers....

you can do this like that:

quotient = (float) integer1 / (float) integer2;

( just replace that line with this)

i tried to explain this in a very simply and easy way i hope i didn't get you too confused...

good luck...

Regards,
mathe917
• 01-22-2002
_Will333_
yup

int / int = int
9 / 2 = 4

double / double = double
9.0 / 2.0 = 4.5

and double / int or int / double = double

9.0 / 2 = 4.5, 9 / 2.0 = 4.5

you can either typecast
double(int_variable)

or multiply one side by 1.0...which is basically the same thing...but may be a bit harder to understand for others.