Thread: cout<<"Error";

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    cout<<"Error";

    why does
    Code:
    cout<<149/50;
    print 2 not 2.98?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because 149 and 50 are both integers, so the result of the division is an integer.

    You have to use floating point types to get the right answer. Do 149.f/50.f instead.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    cout << 149./50;
    or
    cout << 149.0/50;
    or
    cout << (float)149/50;
    or
    cout << (double)149/50;

    will give you 2.98

    (Anything that will tell the compiler to convert the result to a floating point type.)

  4. #4
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    cout<<float(149)/50;

  5. #5
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    *meep*
    thanks whoever posted first :-p
    Last edited by krappykoder; 03-01-2003 at 07:41 AM.

Popular pages Recent additions subscribe to a feed