Thread: New to C++ Classes

  1. #46
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What is restart? I assume it is a single character. If that's the case, then you need to use single quotes around 'y' and 'Y' to indicate that they are characters as well. The double quotes mean they are strings.

  2. #47
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, you could use tolower() from <cctype>, which would allow you to use only one comparison.
    Code:
    if(tolower(restart) = 'y')
    tolower() converts the character passed to it to lowercase. If it's already lowercase or not a letter, it just returns the character it was passed.

    [edit] I mean ==. Whoops. [/edit]
    Last edited by dwks; 06-21-2007 at 02:02 PM.
    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.

  3. #48
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Psst...
    Code:
    if(tolower(restart) == 'y')

  4. #49
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Can I use the tolower for strings also?

  5. #50
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, but you can use it on each character in a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #51
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, compiled and ran. The only thing is, all of the things (money, lives, attack points, and defense points) are way way way higher than they are supposed to be.

    I put the Manager function above the other functions in GameManager.cpp and I am now getting these errors:
    GameManager.h:13: error: ISO C++ forbids initialization of member `money'
    GameManager.h:13: error: making `money' static
    GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'

    for each variable when I print out all the variables when showing the the player stats.

  7. #52
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have a separate class instance for each player, or do you keep all the data in one GameManager?

    Why is money static?

  8. #53
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    There is only one player, the person playing.
    I didn't make money static. The integer money is private though. I have it here:
    Code:
    class GameManager
    {
    private:
           int money = 200;
           int lives = 3;
           int attack = 0;
           int defense = 0;
           string battle;
           string shop;
           char restart;
           string ShopBattle;
    That's the GameManager.h file.


    I don't know if I should give the variables values here or in the Manager() function.

  9. #54
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to initialize the variables in the constructor (which is probably what you meant by "the Manager() function").
    Last edited by Daved; 06-21-2007 at 03:25 PM.

  10. #55
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can initialize class variables in a constructor with the ordinary assignment operator:
    Code:
    class c {
    public:
        int value;
        c(int x);
    };
    
    c::c(int x) {
        value = x;
    }
    Or you can use an initializer list (which you have to use if the variables are constant):
    Code:
    class c {
    public:
        int value;
        c(int x) : value(x);
    };
    
    c::c(int x) {
    
    }
    For multiple variables, the initializer list looks like this:
    Code:
    constuctor() : one(x), two(y), name(firstname) ...;
    The variables should appear in the order they are declared in the class.

    Also, the constructor can be declared inside the class, making it inline. But that doesn't matter if there's nothing in the body of the constructor.
    Code:
    class constructor {
        constructor() : one(x), two(y) {}
    };
    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.

  11. #56
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, it's compiling fine now, thanks for all the help.
    However, the:
    Code:
    int GameManager::GetMoney()
    {
        return money;
    }
    functions aren't working (I have one for each variable). Either that isn't working or the:
    Code:
    void GameManager::AddAttack(int amount)
    {
         attack =+ amount;
    }
    functions aren't working.
    Maybe both. I know this because when I played the game I beat every single robot even though I shouldn't and when I should have won money the amount in the stats stayed the same. If you need me to post the code I will.

  12. #57
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If you need me to post the code I will.
    Maybe that would be best.

    >> attack =+ amount;
    Make sure you copy and paste to prevent typos.

  13. #58
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    GameManager.cpp
    Code:
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include "GameManager.h"
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    void GameManager::Manager()
    {
         int money = 200;
         int lives = 3;
         int attack = 0;
         int defense = 0;
         ShopClass shop;
         BattleClass battle;
         cout << "Current Status:\n";
         cout << "Money: $" << money << "\n";
         cout << "Lives: " << lives << "\n";
         cout << "Attack Points: " << attack << "\n";
         cout << "Defense Points: " << defense << "\n";
         cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
         cin >> ShopBattle;
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        shop.Shop();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                battle.Battle();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    
    int GameManager::GetMoney()
    {
        return money;
    }
    int GameManager::GetAttack()
    {
        return attack;
    }
    int GameManager::GetDefense()
    {
        return defense;
    }
    int GameManager::GetLives()
    {
        return lives;
    }
    
    void GameManager::AddMoney(int amount)
    {
         money =+ amount;
         if (money < 0)
              money = 0;
    }
    
    void GameManager::Lives()
    {
         GameManager manager;
         lives =- 1;
         if (lives == 0)
            cout << "Game Over\n";
            cout << "Would you like to start again? (y or n)";
            cin >> restart;
            if (tolower(restart) == 'y') {
               money = 200;
               lives = 3;
               attack = 0;
               defense = 0;
               manager.Manager();
            } else if (tolower(restart) == 'n') {
                   
            } else {
                   cout << "Error: Invalid Input\n";
                   manager.Lives();
            }
    }
    
    void GameManager::AddAttack(int amount)
    {
         attack =+ amount;
    }
    
    void GameManager::AddDefense(int amount)
    {
         defense =+ amount;
    }
    ..and an example of code that needs to use some of the variables
    Code:
     if (manager.GetAttack() >= 1){
               cout << "Attack Successful\nYou Won $200\n\n";
               manager.AddMoney(200);           
            } else {
              cout << "You Lost\n\n";
              manager.Lives();
            }

  14. #59
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> attack =+ amount;
    That compiles?

  15. #60
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Yeah, should I make it:
    attack = attack + amount?

    I probably picked that up from Java :x

    Still wont work.
    Last edited by Cypher; 06-21-2007 at 08:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM