What is wrong with this code?

This is a discussion on What is wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; I just started C++ and I bought a few books. One of them, which I'm reading right now, has this ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    What is wrong with this code?

    I just started C++ and I bought a few books. One of them, which I'm reading right now, has this code that I'm supposed to type in and learn how it works, but it has 4 errors when I compile:

    Code:
    #include <iostream.h>
    class EnergyLevel
    {
    public:
    	int Level;
    };
    
    
    main()
    {
    EnergyLevel good;
    good.Level = 10;
    cout << "Good's energy level is at ";
    cout << good.Level << endl;
    good.Level ++;
    cout << "Good's energy level is now up to ";
    cout << good.Level << endl;
    }
    I typed all of it in as it is in the book. What's wrong with it? This book is from 1994 so I'm hoping the code the author is using isn't outdated or something.

    Just in case you need to know, here are the errors:

    (2) : error C2061: syntax error : identifier 'EnergyLevel'
    (2) : error C2059: syntax error : ';'
    (3) : error C2449: found '{' at file scope (missing function header?)
    (6) : error C2059: syntax error : '}'
    Last edited by Tride; 10-27-2003 at 10:53 AM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The code is outdated...
    Code:
    #include <iostream>
    using namespace std;
    
    class EnergyLevel
    {
    public:
    	int Level;//this might work but its not good style...
    };
    
    
    int main()
    {
    EnergyLevel good;
    good.Level = 10;
    cout << "Good's energy level is at ";
    cout << good.Level << endl;
    good.Level ++;
    cout << "Good's energy level is now up to ";
    cout << good.Level << endl;
    }
    You should learn pretty soon (if the book is worth anything) that you don't make data members public...instead you declare public functions that return the values:

    Code:
    class SomeClass
    {
    public:
    SomeClass(int val) { itsval = val; }
    ~SomeClass() {}
    int GetVal() { return itsval;}
    private:
    int itsval;
    };
    I would recommend getting a newer book that is up to the standards (headers that don't have .h on the end)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    49
    Thanks, maybe I'll get a new book. I was hoping this one would be really good because the reviews I've seen for it have said that it's a very good book for learning C++.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    49
    One last question, when I delete the .h from iostream I get this error:

    fatal error C1189: #error : "eh.h is only for C++!"

    Why does it do this?

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    This is the cause of all your problems -- your compiler is trying to compile this as C code, not C++ code.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    49
    Oh, well that's good to know

    Do you have any idea how I might change that? I'm using Microsoft Visual C++. I don't know how similar compilers are, but if you're not using the same one do you have an idea of what I have to do? I don't want to mess anything up.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    is your file saved as a .cpp file?

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    49
    Ahh, that was my problem. Sorry, I just started programming in C++ after learning C for around half a year.

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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