problem with class

This is a discussion on problem with class within the C++ Programming forums, part of the General Programming Boards category; hey, I'm making a crappy little rpg text game. Anyway I set it up so that the player stats are ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    problem with class

    hey,
    I'm making a crappy little rpg text game. Anyway I set it up so that the player stats are all saved into a class. I have a function which creates the class when a new game is started. The problem is that I can only access the class functions through the function which I used to create it.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    #include <string>
    class character{
    public:
    int health;
    int strength;
    int armor;
    string name;
    
    character(int, int, int, string);
    ~character();
    void viewStats();
    string saveStats();
    };
    
    character::character(int h, int s, int a, string n){
    health = h;
    strength = s;
    armor = a;
    name = n;
    }
    character::~character(){
    }
    void character::viewStats(){
    cout << "\n";
    cout << "Name: " << name << endl;
    cout << "Health: " << health << endl;
    cout << "Strength: " << strength << endl;
    cout << "Armor: " << armor << endl;
    }
    string character::saveStats(){
    string send;
    send += name + "#";
    send += health + "#";
    send += strength + "#";
    send += armor + "#";
    return send;
    }
    
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    void newGame(){
    string temp;
    int a, b, c;
    cout << "\nName:";
    cin.get();
    getline(cin, temp, '\n');
    again:
    a = GetRand(10, 15);
    b = GetRand(10, 15);
    c = GetRand(50, 70);
    character player(c, a, b, temp);
    player.viewStats();
    cout << "\n\nNew Stats (Y\\N)\n\n";
    char choice;
    cin >> choice;
    if(choice == 'y' || choice == 'Y'){
    goto again;
    }
    
    }
    
    void clear(){
    for (int i =0; i < 25; i++){
    cout << "\n";
    }
    }
    void sort(string command){
    int which;
    which = command.find("\\help", 0);
    if(which == 0){
    cout << "Help menu!";
    return;
    }
    which = command.find("\\save", 0);
    if(which == 0){
    cout << player.saveStats();
    }
    }
    
    int main(int argc, char *argv[])
    {
    int choose;
    string command;
    cout << "Menu:\n\n1.New Game\n2.Load\n3.Quit\n\n";
    cin >> choose;
    switch (choose)
    {
    case 1:
    newGame();
    break;
    }
    clear();
    cout << "Welcome to this crappy RPG game. To see the commands type \\? or \\help\n\n";
    cout << "It\'s recommended that you save. You can save at any time by typing \\save\n\n";
    cin.get();
    getline(cin, command, '\n');
    sort(command);
    cin >> choose;
      return 0;
    }
    I realize this code is probably written very poorly. Anyway, the function that you should worry about is newGame();

    Any help would be apreciated thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    One way is to make a Game class that keeps the character as a member (or a container of characters as a member). Then all the game related function could be members of the Game class and can use the member variable that holds the character.

    Another way is to pass a reference or pointer to the new character around. If you did this you would have to dynamically allocate (with new) the character you create in newGame() and remember to delete it later, which is dangerous. Or you could create the character in main and then pass it by reference to the other functions.

    I like the first solution better.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    thanks

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    41
    Umm, I dont have a problem with it, but some people really get worked up when you use goto's

    So if it worries you, why not do something like:
    Code:
    int busy=1;
    char choice;
    int main(){
    while (busy==1){
    //game code goes here
    if (choice=="n"){
    busy=0;
    break;
    }
    }
    }
    Hope it helps

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    yea, I've heard that about gotos
    why is that?

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Because sometimes it creates "sphaggetti code" where its all mixed up and hard to tell where to go to next.

    Speaking of code thats hard to read...indent please, it will help yourself and people viewing your code.
    Last edited by JoshR; 07-08-2005 at 07:05 PM.

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Goto is also unnecessary in almost every situation in C++, there are however 1/10000 situations where it might be more clear to use a goto.

    Its also said that sometimes a goto could result in unpredictable results.

    www.cprogramming.com/tutorial/goto.html

    http://www.gmonline.demon.co.uk/cscene/CS7/CS7-02.html // example where it could be the "best choice to use it".
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    20

    Thumbs up Searching really works!

    Hi there, I'm new.

    I search the c++ forum for "class", pick the first topic, and what do I find?
    Someone with the exact same problem I am having!

    I am writing a texst/rpg-type game, and I would like a funktion to be able to access the public variables of an object.

    Now, I think Daved said that it could be done, by passing a refference to the object, that had to be deleted later, and is dangerous.
    Ehh, how dangerous is it? And What could it possibly do?

    Is there perhaps a safer way, to only let funktions access the public methods of the object? Or would that need the dreaded refference too?

    Thank you in advance!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you're both doing it poorly. Your data doesn't need to be public, you just need public access to it. Consider:
    Code:
    class Foo
    {
        private:
            int bar;
    
        public:
            void setBar( int baz ) { bar = baz; }
            int getBar( void ) { return bar; }
    };
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    using quzah's class, you can do it this way:
    Code:
    #include<iostream>
    
    class Foo
    {
            private:
                    int bar;
            public:
                    void setBar( int baz ) { bar = baz; }
                    int getBar( void ) { return bar; }
    };
    
    void changeFoosBar(int changeTo,Foo*target);
    
    int main()
    {
                    Foo *fooClass=new Foo();
                    fooClass->setBar(0);
                    changeFoosBar(20,fooClass);
                    std::cout<<fooClass->getBar()<<std::endl;
                    delete fooClass;
                    return 0;
    }
    
    void changeFoosBar(int changeTo,Foo*target)
    {
            target->setBar(changeTo);
    }
    or if you really want to access them directly (which I highly recommend you don't do), you can just pass them in by reference:
    Code:
    #include<iostream>
    
    class Foo
    {
            public:
                    int bar;
    };
    
    void changeFoosBar(int changeTo,int&changeThis);
    
    int main()
    {
                    Foo fooClass;
                    fooClass.bar=0;
                    changeFoosBar(20,fooClass.bar);
                    std::cout<<fooClass.bar<<std::endl;
    
                    return 0;
    }
    
    void changeFoosBar(int changeTo,int&changeThis)
    {
            changeThis=changeTo;
    }
    Last edited by major_small; 07-09-2005 at 10:25 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Why did you use dynamic allocation for the first one major instead of passing the class by reference?
    Woop?

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by prog-bman
    Why did you use dynamic allocation for the first one major instead of passing the class by reference?
    just because.
    Last edited by major_small; 07-09-2005 at 10:37 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Nerd.
    Woop?

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if it bothers you that much (and because it'd probably be easier for the OP):
    Code:
    #include<iostream>
    
    class Foo
    {
            private:
                    int bar;
            public:
                    void setBar( int baz ) { bar = baz; }
                    int getBar( void ) { return bar; }
    };
    
    void changeFoosBar(int changeTo,Foo &target);
    
    int main()
    {
            Foo fooClass;
            fooClass.setBar(0);
            changeFoosBar(20,fooClass);
            std::cout<<fooClass.getBar()<<std::endl;
            return 0;
    }
    
    void changeFoosBar(int changeTo,Foo &target)
    {
            target.setBar(changeTo);
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    OK, kewl. So that is how it is done.
    Then I hear that they have to be destroyed again, or the undefined "danger" will arrise...
    Does that mean the pointer/refference itself or the entire object?
    And how is it done?

    Oh, and thanks again!

    edit/ Eh, I know how to destroy an object, but if its the refference, I'd still like a shot demonstration...
    Last edited by muggizuggi; 07-09-2005 at 11:24 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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