Thread: Expressions in cout statement vs variable

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Expressions in cout statement vs variable

    Hello all.

    Is it better programming practice to

    1) output the value of an expression a * b in a cout statement as a variable containing its value
    Code:
       total = a * b;
       cout << total << endl;
    or

    2) is printing value of the expression as calculated directly from the cout statement preferred?
    Code:
       cout << a * b;
    I searched around, but couldn't find any specific information regarding this. I've seen both in code.

    Thank you for your time!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you need the total besides for output? If so, then the former is appropriate, otherwise the latter is appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    The only time I need the total is for output.

    Thank you for your response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable declarations and arithmetic expressions
    By r3zaneo in forum C Programming
    Replies: 3
    Last Post: 10-21-2012, 01:36 PM
  2. if statement with hex variable
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 04-18-2012, 03:36 AM
  3. Replies: 1
    Last Post: 07-15-2011, 10:46 AM
  4. declare variable in case statement
    By Marksman in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 03:32 AM
  5. Evaluating multiple expressions in an If statement.
    By bitslap in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 04:38 AM

Tags for this Thread