cout<<"Error";

This is a discussion on cout<<"Error"; within the C++ Programming forums, part of the General Programming Boards category; why does Code: cout<<149/50; print 2 not 2.98?...

  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
    595
    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 06:41 AM.

Popular pages Recent additions subscribe to a feed

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