Life sim C++programming tips

This is a discussion on Life sim C++programming tips within the C++ Programming forums, part of the General Programming Boards category; I created a program that is completely pointless. I'm still learning c++ so i wrote this program to see if ...

  1. #1
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Life sim C++programming tips

    I created a program that is completely pointless. I'm still learning c++ so i wrote this program to see if i could do it and learn a little along the way. It's a life simulator, a small life simulator

    Tell me what i can do better, thanks.

    It's attached to this thread.
    Attached Files Attached Files

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why do you use C-style strings and not C++ strings?
    Why don't you check the variable limits in your set functions?
    Can Hunger be set below 0? Shouldn't you check it? What are the set function for anyway?
    And of cource making User a global variable - I don't like it

    You can put all your activities functions in some class Game
    make person a member of this class
    In this case you don't need to access the global variable User - just a member...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    One more thing. Split the program into a number of files. Working with a single file can get really annpying when it comes to debug time , especialy when you are writing such a large program.

    Your class definition could be placed into one header file, and then create a seperate .cpp file with the class implementation. Also, dont use global pointers. I think vart touched on this.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by vart
    Why do you use C-style strings and not C++ strings?
    Why don't you check the variable limits in your set functions?
    Can Hunger be set below 0? Shouldn't you check it? What are the set function for anyway?
    And of cource making User a global variable - I don't like it

    You can put all your activities functions in some class Game
    make person a member of this class
    In this case you don't need to access the global variable User - just a member...
    what's the difference between a c-style string and a c++ style string?

    i guess i could check the limits in my set functions instead of the check functions that'd probably save on space.

    i do check hunger, it's in this function:

    Code:
    void hungerCheck(int transferHunger) //checks if your fat or skinny
    {
         int hCheck;
         hCheck = User.GetHunger();
         hCheck += transferHunger;
         if( hCheck> 15)
         {
                    death(1);
         }
         else if(hCheck < 0)
         {
                    death(9);
         }
         
         else
         {
                    User.ChangeHunger(transferHunger);
         }
    }
    that means if hunger goes below zero then death(9) (gluttony) is called

    and i get what your saying with the class game thing, i'll go work on that.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    C style string:

    Code:
    char name[30];
    A C style string is a char array. The problem is it is limited to the amount
    of space allocated by the programmer ( you ) in the brackets.

    C++ string:

    Code:
    string name;
    A C++ string has no character amount limits. Thus, making it much more
    flexable than the C array style version. If you want to write a line with a space
    you can use the getline() function of that.
    Remember to #include <string> when you use C++ strings.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I think I would put the check of bounds inside the SetHunger function and return state
    state will be enum like
    Code:
    enum LifeState
    {
       helthy =0,
       death_of_hunger,
    ...
       death_of_overwight
    }
    in this case function hungerCheck
    can be made like:
    Code:
    
    LifeState state = SetHunger (value);
    if(state != helthy ) death(state);
    All other set function will return other values of this state enum
    And other checks could be made in the same way
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    nice work though, for a beginner

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Also you should use <cstring> instead of <string.h> in C++ programs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 03:30 PM
  2. Artificial Life: Where to Start?
    By Mr.Sellars in forum General AI Programming
    Replies: 11
    Last Post: 09-22-2007, 03:03 AM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 03:17 PM
  4. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 08:53 PM
  5. Life, The Universe, and everything else
    By ZooTrigger1191 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-29-2003, 05:33 PM

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