A "cout" question

This is a discussion on A "cout" question within the C++ Programming forums, part of the General Programming Boards category; How do I make it so that this statement shows .8 instead of 0: cout << 4/5; It keeps displaying ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    12

    A "cout" question

    How do I make it so that this statement shows .8 instead of 0:
    cout << 4/5;

    It keeps displaying 0 instead of .8. Thanks for your help.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    cout<<4.0/5.0;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    12
    Now how would I do that if it were a variable?

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    If it were a variable, the << operator would know how. If both top/bottom are floats, it will output 0.8. But if they are ints, then you will get the result of integer division, which drops the remainder and the << will output an int, or 0.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Type cast it. I believe C++ likes you to use "static_cast". IIRC, it's something like:
    Code:
    int var1 = 4, var2 = 5;
    cout << static_cast<float>(var1) / static_cast<float>(var2);
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    12
    Here is my line of code:

    cout << "PI= " << 4 * static_cast<float>(c) / static_cast<float>(n) << endl;
    Both of the variables, c & n, were defined as integers. I still can't get the line to print out any decimals. I'm going to fiddle around with it, but maybe you know what I am doing wrong? Thanks for all your help already quzah.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you look at that line, you'll see that 4 isn't a floating point number. Cast it also, or just ad a .0 to it.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    12
    Thanks for your help, it worked!

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by scrub05
    Both of the variables, c & n, were defined as integers. I still can't get the line to print out any decimals.
    Even casting and performing a floating point division operation might result in no decimals being output if the result of that operation results in a value such as X.0. The decimals will not be shown if there isn't anything to show or unless you tell cout to format its output differently:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        float f = 5.0f;
        cout << "f is: " << f << endl;
        cout << setiosflags(ios::fixed) << setprecision(4)
             << "f is: " << f << endl;
    
        int c = 8, n = 4;
        float result = 4 * static_cast<float>(c) / static_cast<float>(n);
    
        cout.unsetf(ios::fixed);
        cout << "Result is: " << result << endl;
        cout << "Result is: " << setiosflags(ios::fixed)
             << setprecision(2) << result << endl;
    
        return 0;
    }
    My output:
    Code:
    f is: 5
    f is: 5.0000
    Result is: 8
    Result is: 8.00
    So you see it can work but depending on what your values for c and n are you might not be seeing the decimals.
    Last edited by hk_mp5kpdw; 01-06-2005 at 03:54 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I'd sooner write a function which prototypes the arguments as Double, as well as returns a Double (regardless what type you passed to it). Then allow C++'s Promotion Rules to do the work for you. Much more elegant IMHO. Here's a sample proggie...

    Code:
    #include <iostream>
    using namespace std;
    
    double divide ( double, double );
    
    int main()
    {
        int x, y;
        
        x = 4;
        y = 5;
        
        cout << "4 / 5 = " << divide ( x, y );
        
        while ( cin.get() != '\n' );
        
        return 0;
        
    }
    
    double divide ( double a, double b )
    {
        return a / b;
        
    }
    Last edited by Scribbler; 01-06-2005 at 05:53 PM.

  11. #11
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I woudl just use a solution like what XSquared Said; define your int as a float,

    ie- Intstead of INT X call it Float X. That should get you squared away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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