Newb needs help

This is a discussion on Newb needs help within the C++ Programming forums, part of the General Programming Boards category; hey all im not that new i started awhile ago but quit and now im starting over from the begining. ...

  1. #1
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24

    Unhappy Newb needs help

    hey all im not that new i started awhile ago but quit and now im starting over from the begining. I'm learning C++ from a book "C++ for dummies." Right now im at

    Listing 2-3: "copying a value from one Variable to Another"

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int main()
    {
        int start = 50;
        int finish;
        finish = start;
        cout << finish << endl;
        getch ();
        return 0;
    }
    now i was good at the begining of the variable section but now this part has completly lost me. Can anyone help me plz

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Inferno,

    As long as the variables are of the same type, they can pass values between them. Or, put another way, in the above code, start is declared as type 'int'. It is 'declared' as type int, but also 'initialised' in the same instance. You could have also written this:
    Code:
    int start;
    start = 50;
    They way it is written in the book simply does both steps at once. The next line 'declares' the integer varible 'finish'. No value is assigned to 'finish' at this point. On the very next line though, it is assigned a value. That value is whatever the value of 'start' is. 'start' was declared and initialised on the same line, to the value of 50. That is 'start's' value, so finish now has the value 50 also.
    Just a point that you may not be aware of. In C and C++, the = sign does not mean "is equal to" like in traditional maths. In C/C++, this symbol meas, take the value that is on the right of the symbol (=) and make it the new value of the varible that is on the left.

    I hope I helped,

    Nick.

    (As far as cout << goes, don't worry about it yet. Read on and it'll all make sense later.)
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Think of variables as boxes that can hold one thing, and one thing only. Here, I'll draw an illustration to show it better:

    Illustration 1:
    Ok, so we've got the variable start and finish, at this stage each one is, well, empty, because we haven't assigned anything to them.
    Code:
     int start;
     int finish;
    So we create the two variables, now we can put stuff in them.

    Illustration 2:
    Code:
     int start = 50;
    This code puts the value 50 inside of the start box. As you can see in the illustration, there is now a 50 inside of the start box.

    Illustration 3:
    Code:
     finish = start;
    This is the fun part, in this code it does 2 things. First it gets the value of start by "looking" in the box at the contents of it. We know that start contains the value 50, so basically what we have now is this:
    Code:
     finish = 50;
    Then the next step works just like when we initialized the start variable at the beginning. It merely places the value 50 in to the finish box, making finish "equal" 50.

    -Hope that helps!
    Attached Images Attached Images  

  4. #4
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    Yes it did help....thnxs i appriciate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-23-2008, 12:37 AM
  2. Dogpile the newb!
    By lindy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2008, 09:17 AM
  3. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 02:45 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 09:44 PM
  5. if your not newb you can help me
    By Klinerr1 in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2002, 01:09 AM

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