Help please with something pretty simple

This is a discussion on Help please with something pretty simple within the C++ Programming forums, part of the General Programming Boards category; I'm trying to finish a homework problem that requires an integer as an input but the output result must be ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Question Help please with something pretty simple

    I'm trying to finish a homework problem that requires an integer as an input but the output result must be in decimal form.
    Here's what I have...the program will run & works except for the decimal for the quotient.

    [code]
    int num1, num2;
    float sum, difference, product, quotient;
    sum = num1 + num2;
    difference = num1 - num2;
    product = num1 * num2;
    quotient = num1 % num2;
    cout << "This program computes the sum, difference, product, and quotient of any two integers" << endl;
    cout << "enter an integer then press <Enter>: ";
    cin >> num1;
    cout << "enter your second integer then press <Enter>: ";
    cin >> num2;

    // Add num1 + num2 for the sum.
    sum = num1 + num2;

    // Subtract num2 from num1 for the difference.
    difference = num1 - num2;

    // Multiply num1 x num2 for the product.
    product = num1 * num2;

    // Divide num1 by num2 for the quotient.
    quotient = num1 / num2;

    // Display the sum of num1 + num2.
    cout << "The sum of your two numbers is " << sum << endl;
    cout << "The difference of your two numbers is " << difference << endl;
    cout << "The product of your two numbers is " << product << endl;
    cout << "The quotient of your two numbers is " << quotient << endl << endl;
    return 0;
    }

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    When you have two integers and the operation on them has to result in floating-point, cast one of the integers to double or float:
    Code:
    quotient = static_cast<float>(num1) / num2;
    But not the whole operation or you won't get the precision you want. The result of an integer operation will be cast to float instead of the operation being performed as float.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thank you, thank you, thank you, twm!!!

    That works perfectly. No wonder I couldn't figure it out..we haven't covered <static_cast> in class yet.

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >we haven't covered <static_cast> in class yet.
    Well, you can do it like this too:
    Code:
    // Old style cast
    quotient = (float)num1 / num2;
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    That's what I was trying to accomplish by putting

    int num1, num2;
    float sum, difference, product, quotient;

    but that didn't make it work.

    I was mising the float in quotient = (float) num1 / num2;

    Thanks again!

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    (float)num is the C style of doing it.

    C++ also supports float(num).
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 07:42 AM
  2. Replies: 1
    Last Post: 10-17-2006, 08:03 AM
  3. I need help with a pretty simple program please
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 10:53 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

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