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.
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.
Also, you could use tolower() from <cctype>, which would allow you to use only one comparison.
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.Code:if(tolower(restart) = 'y')
[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.
Psst...
Code:if(tolower(restart) == 'y')
Can I use the tolower for strings also?
No, but you can use it on each character in a string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Do you have a separate class instance for each player, or do you keep all the data in one GameManager?
Why is money static?
There is only one player, the person playing.
I didn't make money static. The integer money is private though. I have it here:
That's the GameManager.h file.Code:class GameManager { private: int money = 200; int lives = 3; int attack = 0; int defense = 0; string battle; string shop; char restart; string ShopBattle;
I don't know if I should give the variables values here or in the Manager() function.
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.
You can initialize class variables in a constructor with the ordinary assignment operator:
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); }; c::c(int x) { value = x; }
For multiple variables, the initializer list looks like this:Code:class c { public: int value; c(int x) : value(x); }; c::c(int x) { }
The variables should appear in the order they are declared in the class.Code:constuctor() : one(x), two(y), name(firstname) ...;
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.
OK, it's compiling fine now, thanks for all the help.
However, the:
functions aren't working (I have one for each variable). Either that isn't working or the:Code:int GameManager::GetMoney() { return money; }
functions aren't working.Code:void GameManager::AddAttack(int amount) { attack =+ amount; }
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.
>> 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.
GameManager.cpp
..and an example of code that needs to use some of the variablesCode:#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; }
Code:if (manager.GetAttack() >= 1){ cout << "Attack Successful\nYou Won $200\n\n"; manager.AddMoney(200); } else { cout << "You Lost\n\n"; manager.Lives(); }
>> attack =+ amount;
That compiles?
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.