#include <iostream>
enum PTYPE { Glock, Deagle, Magnum };
bool ERROR = false;
bool fQuit = false;
class BaseWeapon
{
public:
virtual void SetDamage(int a);
virtual int GetDamage();
protected:
int itsDamage;
};
int BaseWeapon::GetDamage()
{
return itsDamage;
}
void BaseWeapon::SetDamage(int a)
{
a = itsDamage;
}
class Pistol : public BaseWeapon
{
public:
PTYPE GetPistolType();
int ShootWeapon();
PTYPE GetWeapon() { return itsType; }
protected:
PTYPE itsType;
};
PTYPE Pistol::GetPistolType()
{
std::cout << "(1)Glock (2)Deagle (3)Magnum (0)Quit: " << std::endl;
for (;;)
{
int temp;
std::cin >> temp;
switch (temp)
{
case 1:
{
PTYPE itsType = Glock;
break;
}
case 2:
{
PTYPE itsType = Deagle;
break;
}
case 3:
{
PTYPE itsType = Magnum;
break;
}
case 0:
{
bool fQuit = true;
break;
}
default:
{
ERROR = true;
std::cout << "Please enter a valid argument: " << std::endl;
break;
}
}
if (ERROR == true)
continue;
else
break;
}
return itsType;
}
int main()
{
std::cout << "You are walking around and you see a ... gun, ";
std::cout << "they are a Glock, Magnum and Deagle, which one do you choose?" << std::endl;
Pistol *pPistol = new Pistol;
PTYPE weapon;
weapon = pPistol->GetPistolType();
std::cout << "weapon: " << weapon << std::endl;
return 0;
}
[edited by - ThomasSauder on October 12, 2003 11:39:26 PM]
Quick source question
Hey, I have used enum to set different weapon types. Now, the following code SHOULD return the weapon type to PTYPE weapon... but when I'm displaying it, all it shows is -84010201 or the likes..
here's the code.
When you declare itsType inside the switck block, it''s local to that block (to tell the truth, I''m surprised you didn''t get a compile error). I think you have to say PTYPE itsType before your switch statement. Though, to tell the truth, there''s something lacking in your code: when do you ever test fQuit to get out of your infinite loop?
Happy thanksgiving!
now to your question.
fQuit isn''t used yet, because I haven''t implemented it yet :D
I''m just trying to get this to work first.
Cheers
now to your question.
fQuit isn''t used yet, because I haven''t implemented it yet :D
I''m just trying to get this to work first.
Cheers
Thanks for the tip... as you can tell it''s late haha. Stupid me declared a new function, PTYPE itsType in the switch statements, and returned it, whereas I should have declared a new instance of Pistol, and do this:
Pistol tPistol;
tPistol.itsType = Glock etc....
return tPistol.itsType.
Sorry, my stupidity.
Cheers. Where in Ontario are you??
Pistol tPistol;
tPistol.itsType = Glock etc....
return tPistol.itsType.
Sorry, my stupidity.
Cheers. Where in Ontario are you??
Sorry about that.. After a few modifications... here is the right code:
[edited by - ThomasSauder on October 12, 2003 12:03:00 AM]
#include <iostream>enum PTYPE { Glock, Deagle, Magnum };bool ERROR = false;bool fQuit = false;class BaseWeapon{public: virtual void SetDamage(int a); virtual int GetDamage();protected: int itsDamage;};int BaseWeapon::GetDamage(){ return itsDamage;}void BaseWeapon::SetDamage(int a){ a = itsDamage;}class Pistol : public BaseWeapon{public: PTYPE GetPistolType(); int ShootWeapon(); PTYPE GetWeapon() { return itsType; }protected: PTYPE itsType;};PTYPE Pistol::GetPistolType(){ std::cout << "(1)Glock (2)Deagle (3)Magnum (0)Quit: " << std::endl; Pistol tPistol; for (;;) { int temp; std::cin >> temp; switch (temp) { case 1: { tPistol.itsType = Glock; break; } case 2: { tPistol.itsType = Deagle; break; } case 3: { tPistol.itsType = Magnum; break; } case 0: { bool fQuit = true; break; } default: { ERROR = true; std::cout << "Please enter a valid argument: " << std::endl; break; } } if (fQuit == true) { std::cout << "Are you sure you want to quit? (y) - (n): "; char choice; std::cin >> choice; if (choice == 'y') { break; } else { continue; } } if (ERROR == true) continue; else break; } return tPistol.itsType;}int main(){ std::cout << "You are walking around and you see a ... gun, "; std::cout << "they are a Glock, Magnum and Deagle, which one do you choose?" << std::endl; Pistol *pPistol = new Pistol; PTYPE weapon; weapon = pPistol->GetPistolType(); if (weapon != 0 && weapon != 1 && weapon != 2) { std::cout << "Good Bye!" << std::endl; std::cout << "I hope you had a fun time in this game\n"; return 0; } std::cout << "weapon: " << weapon << std::endl; return 0;}
[edited by - ThomasSauder on October 12, 2003 12:03:00 AM]
With the new code, don''t you get errors about trying to access/modify a protected member variable?
I''m not sure, but are trying to modify pPistol->itsType? If so, don''t declare a new pistol inside GetPistolType(). To do that, just change the "PTYPE itsType = ..." lines from your original code to "itsType = ...".
I''m not sure, but are trying to modify pPistol->itsType? If so, don''t declare a new pistol inside GetPistolType(). To do that, just change the "PTYPE itsType = ..." lines from your original code to "itsType = ...".
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement