Help with Constants and enum constants. Newbie learning C++.

This is a discussion on Help with Constants and enum constants. Newbie learning C++. within the C++ Programming forums, part of the General Programming Boards category; Okay, I am learning C++ using the tutorial on this site and also reading learning c++ in 21 days. I ...

  1. #1
    UnregJDiPerla
    Guest

    Help with Constants and enum constants. Newbie learning C++.

    Okay, I am learning C++ using the tutorial on this site and also reading learning c++ in 21 days.

    I am up to the part of variables and constants. It says that a variable can be defined and initialised from the beginning like this:

    Ulong myAge = 23;

    But the book also described it as a constant. Can someone please explain the difference. I am not getting this very well.

    Also, about enumerated constants, I am having problems understanding this. Can someone explain this to me as well? I tried to do some code like this:

    #include <iostream.h>
    typedef unsigned short int Uint;

    void main()
    {

    enum Jerky{Uint j=1, Uint o=2, Uint e=3};

    int x;
    Jerky joey;

    joey=Jerky(3);

    cout <<joey;
    return 0;
    }

    I am using Visual C++ 6.0 introductory edition. It gives me the following errors:

    C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(7) : error C2061: syntax error : identifier 'j'
    C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : error C2146: syntax error : missing ';' before identifier 'joey'
    C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : error C2501: 'Jerky' : missing storage-class or type specifiers
    C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    enumsvare.obj - 4 error(s), 0 warning(s)

    can someone tell me what I did wrong here?

    Thank you for your time in advance.

    Joey

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main()
    is wrong. Use int main(). If your book suggests void main, maybe its time to change book.

    >>enum Jerky{Uint j=1, Uint o=2, Uint e=3};
    This may be better:
    >>enum Jerky {j=1, o=2, e=3};
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    UregJDiPerla
    Guest

    Thanks, it worked.

    Thanks it worked!!! but can someone still give me a bit of explanation on these things or maybe where I could find a good explanation on the web?

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    82

    Re: Help with Constants and enum constants. Newbie learning C++.

    Originally posted by UnregJDiPerla
    [B]I am up to the part of variables and constants. It says that a variable can be defined and initialised from the beginning like this:

    Code:
    Ulong myAge = 23;
    But the book also described it as a constant. Can someone please explain the difference. I am not getting this very well.
    Hmmm...maybe the book is referring to the 23 as the constant...I don't have my copy of C++ in 21 days with me, but myAge certainly is not a constant as it is defined.

    Also, about enumerated constants, I am having problems understanding this. Can someone explain this to me as well?
    Enumerations basically set up aliases so that you can use them interchangeably with numbers in the code. In other words, as you set it up, wherever the compiler sees "j", it will treat it as if you said "1". Once you have defined an enumeration, the variables in that enumeration are constant, so you can't increment them, assign to them, etc.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    How is enum better or different from using const to define constants?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    C++ IS A GREAT BOOK!

    IMHO, "C++ in 21 Days" is a GREAT book!

    I don't have my copy with me either... too embarrased to have a book with that title at work I don't remember if Mr. Liberty uses void main() but the self-guided-course style of the book is excellent. The end of the chapter questions and problems tell you if you've learned what you're supposed to.

    I wish there were more advanced books with the same approach. It's much more difficult to go through Petzold's "Programming Windows" trying to make-up your own exercises and wondering if you really get-it.

    I was surprised to find myself using the book as a reference well beyond "21 days". If the answer is in there, it's easy to find and understand. I really should get a copy for work, although I do most of my programming at home.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum constants as array indices
    By hzmonte in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 07:23 PM

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