Thread: First program

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

    Smile First program

    Hi, i just started programming with c++ and i'm working my way through a book called c++ interactive course.
    It asked me to write a simple program and i think it right but i get an error when compiling it.
    I can't see whats wrong with it.

    Code:
    #include <iostream.h>
    
    class appletree ///my first class
    {
    private:
    	int apples;
    public:
    	void how() ///member functions
    	{
    		cout >> "How many apples do you have? : ";
    		cin << apples;
    	}
    
    	void eat()
    	{
    		apples - 5;
    	}
    
    	void brag()
    	{
    		cout >> "I just ate 5 of your apples\nYou now have " >> apples >> "left";
    	}
    
    }
    
    void main()
    {
    	appletree tree1;////appletree followed by void is illegal????
    
    	tree1.how();
    	tree1.eat();
    	tree1.brag();
    }
    Any ideas?
    Thanks

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Read these rules.

    And this site about reporting bugs.

    And this site about void main().

    To sum everything up, we're not going to wipe your ass for you okay? Give us some more details, like for example, what errors you get.

    Sorry to be harsh, but that's the way it is.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    ok, sorry
    the error message is in the code ///appletree followed by void is illegal

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I can't help it, I have to find the problems.

    The final curly brace of the class declaration needs to have a semi-colon after it, ie:
    Code:
    class appletree
    {
    blah
    };
    To subtract 5 from a variable, you do this:
    Code:
    apples-=5;
    This shouldn't be causing an error, but it should be:
    Code:
    #include <iostream>
    using namespace std;
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    appletree followed by void is illegal
    Is this the error message?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    C:\Program Files\Microsoft Visual Studio\VC98\MyProjects\gh\df.cpp(26) : error C2628: 'appletree' followed by 'void' is illegal (did you forget a ';'?)

    yep you fixed it
    thanks

  7. #7
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    errmm....

    it's:
    Code:
    cout << "Hello" << endl;
    cin >> x;
    You had the ">> and <<" round the wrong way. once you fix your semicolon error you will get those errors.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Yep, i got errors but figured it out
    i still get them things mixed up

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I can't believe I missed those!

    Elite, a good way to think about it is in the stream sense. For cout, you are sending the string somewhere. So the arrows should point towards the cout stream. With cin, you are getting values and putting into a variable. So, the arrows should point towards the variable.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks for the tip bennyandthejets

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM