Thread: C++ Newbie Question:

  1. #1
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31

    C++ Newbie Question:

    Hi, I have recently started to read some C++ books, and i came across this in the "C++ for dummies"(obviously apt for myself) the following example on Mixed Mode Expressions.

    Code:
    // in the following expression the value of nValue1 
                              // is converted into a double before performing the 
                              // assignment 
                              int nValue1 = 1; 
                              nValue1 + 1.0;
    So i wrote a little output to test, and i cannot seem to get anything other than 1 as my output. Is this correct?

    I would of expected from the comments from the book excerpt above suggest that the integer is converted to a float either as, 1.0 or perhaps if i squint hard it should of returned an integer of 2.

    So can anyone explain what I've interpreted or coded incorrect and shed some light on the subject.?

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    
    int main (int nNumberofArgs, char* pszArgs [])
    
    {
    	int nValue = 1;
    	nValue + 1.0;
    
    	cout << nValue << endl;
    
    	system ("PAUSE");
    	return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You evaluate nValue + 1.0, and when you do so nValue is indeed converted to a double. But you don't change the value of nValue or anything, nor do you store the computed result anywhere.

    Edit: I suppose I should also mention that even if you had stored the result somewhere, it wouldn't matter, because C++ will suppress trailing zeroes on output, so 2.0 will print as just plain "2".
    Last edited by tabstop; 02-20-2009 at 10:56 PM.

  3. #3
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Quote Originally Posted by tabstop View Post
    You evaluate nValue + 1.0, and when you do so nValue is indeed converted to a double. But you don't change the value of nValue or anything, nor do you store the computed result anywhere.

    Edit: I suppose I should also mention that even if you had stored the result somewhere, it wouldn't matter, because C++ will suppress trailing zeroes on output, so 2.0 will print as just plain "2".
    Yeah, thanks for the reply. I thought of both these things just after i posted. And the answer makes sense. So to fix this, do i simply declare another variable to store it? This is what i tried after posting, but i had the same results.

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    
    int main (int nNumberofArgs, char* pszArgs [])
    
    {
    	int nValue = 1;
    	nValue + 4.9;
    
    	double nValue2 = nValue;
    
    	cout << nValue2 << endl;
        system ("PAUSE");
    	return 0;
    I also tried using %d to force a decimal output via cout. That didn't seem to work, but i haven't searched for exact syntax (haven't covered that officially yet) but i think i could fix that.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to store the answer somewhere, rather than just compute it and throw it away. Right now, nValue + 4.9 is evaluated, but nothing happens.

  5. #5
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Quote Originally Posted by tabstop View Post
    You need to store the answer somewhere, rather than just compute it and throw it away. Right now, nValue + 4.9 is evaluated, but nothing happens.
    Yes I understood what you said, and i thought my updated code was attempting to remedy this, but it didn't.. After reading white pages of babel all day, I'm afraid how to store the result seems painfully obvious, But i cannot seem to see how i do that. (at least not unless i already declare it as a float in the first place)

    Any advice or example would be welcomed, As i said I'm a noob that has not even read past chapter 4 yet...

    But thanks anyway, As i wasn't really trying to achieve anything from the code, just understand why it didn't change, and your explanation tells me this. Now i just need to understand everything else to fix it...

    Cheers!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You generally use = to assign values to something; so if you want nValue2 to be assigned the value of nValue + 4.9, you do that:
    Code:
    nValue2 = nValue + 4.9;

  7. #7
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Quote Originally Posted by tabstop View Post
    You generally use = to assign values to something; so if you want nValue2 to be assigned the value of nValue + 4.9, you do that:
    Code:
    nValue2 = nValue + 4.9;
    Thankyou! (Damn Syntax) I did try that as that's what i thought you meant by storing it, VS throughout an error because i stupidly wrote:

    Code:
    nValue + 4.9 = nValue2
    Compiler Error told me something about "left operand must be l-value" So i figured i must be on the wrong track, and maybe i misunderstood.... (It wouldn't be the first time)

    And now that Compiler Error, makes much sense after how you have written it above. Thankyou, at least i know i was on the right track. Much appreciated for your quick replies.

    Cheers.

    The code below now compiles with the answer 5.9 as the output, or 5 if nValue2 is an Integer.

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    double nValue2;
    
    int main (int nNumberofArgs, char* pszArgs [])
    
    {
    	int nValue = 1;
    	nValue2 = nValue + 4.9;
    
    	cout << nValue2 << endl;
        system ("PAUSE");
    	return 0;
    }
    Last edited by Freestyler; 02-21-2009 at 12:25 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Freestyler
    Compiler Error told me something about "left operand must be l-value"
    The "l" in "l-value" means "left hand side". The expression (nValue + 4.9) results in an rvalue, i.e., it can appear on the right hand side of an assignment, but not on the left hand side (lvalues can appear on both sides since they can be converted into rvalues).
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also move nValue2 inside main, since global variables can be bad, and certainly isn't right for your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM