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

This is a discussion on I've compiled, but I don't know what the problem is. within the C++ Programming forums, part of the General Programming Boards category; I haven't had any problems with my other programming, but this one seems to stump me. The program asks to ...

  1. #1
    Registered User
    Join Date
    Jan 2002

    Unhappy 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

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    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....


    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


  3. #3
    Registered User
    Join Date
    Jan 2002
    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
    Enter second integer
    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?

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001


    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.

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    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)

    that will solve your problem....

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

    good luck...


  6. #6
    Registered User
    Join Date
    Jan 2002

    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

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

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "

    - Bertrand Russell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21