帮助,无论何时我运行我的游戏,每当我在武器上调用display()函数时,它总是返回32767!我怎样才能这样做,以便在调用显示功能时,它将武器的力量打印到控制台?提前谢谢!
//a SETIsoft game
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// GLOBAL VARIABLES
string charname;
//NAV CLASS
class nav
{
public:
int navatk;
int navdef;
int navspd;
int navluk;
int navhpo;
int navepo;
//CLASS MEMBER FUNCTIONS
void INIT(int navattack, int navdefense, int navspeed, int navhp, int navluck, int navenergy);
void display();
};
//NAV MEMBER FUNCTION DEFINITIONS
void nav::INIT(int navattack, int navdefense, int navspeed, int navhp, int navluck, int navenergy)
{
navatk = navattack;
navdef = navdefense;
navspd = navspeed;
navhpo = navhp;
navluk = navluck;
navepo = navenergy;
}
void nav::display()
{
cout<<"attack: "<< navatk <<endl;
cout<<"defense: "<< navdef <<endl;
cout<<"speed: "<< navspd <<endl;
cout<<"health points: "<< navhpo <<endl;
cout<<"luck: "<< navluk <<endl;
cout<<"energy points: "<< navepo <<endl;
}
//BUG CLASS
class bug
{
public:
int bugatk;
int bugdef;
int bugluk;
int bugspd;
int bughpo;
int bugepo;
//CLASS MEMBER FUNCTIONS
void INIT(int bugattack, int bugdefense, int bugspeed, int bughp, int bugluck, int bugenergy);
void display();
};
//BUG MEMBER FUNCTION DEFINITIONS
void bug::INIT(int bugattack, int bugdefense, int bugspeed, int bughp, int bugluck, int bugenergy)
{
bugatk = bugattack;
bugdef = bugdefense;
bugspd = bugspeed;
bughpo = bughp;
bugluk = bugluck;
bugepo = bugenergy;
}
void bug::display()
{
cout<<"attack: "<< bugatk <<endl;
cout<<"defense: "<< bugdef <<endl;
cout<<"speed: "<< bugspd <<endl;
cout<<"health points: "<< bughpo <<endl;
cout<<"luck: "<< bugluk <<endl;
cout<<"energy points: "<< bugepo <<endl;
}
//WEAPON CLASS
class userweap
{
public:
int id;
int power;
int cost;
string descrip;
//CLASS MEMBER FUNCTIONS
void INIT(int id, int weappower, int weapcost);
void display();
string equip();
};
//CLASS MEMEBER FUNCTION DEFINITIONS
void userweap::INIT(int id, int weappower, int weapcost)
{
weappower = power;
weapcost = cost;
}
void userweap::display()
{
cout<<"power: " << power <<endl;
}
string userweap::equip()
{
string equipped;
equipped = id;
return equipped;
}
//GLOBAL HELPER FUNCTIONS
void intro();
string choice(nav & user);
void btutorial();
string allgood();
void checkstats(bug & simbug, nav & user);
void simbugmake(bug & simbug);
void regcannonmake(userweap & regcannonweap);
void beamswordmake(userweap & beamsword);
void laserbowmake(userweap & laserbow);
void flyingfistmake(userweap & flyingfist);
void razordiscmake(userweap & razordisc);
void blockpunchmake(userweap & blockpunch);
void flaremake(userweap & flare);
void wavemake(userweap & wave);
void whipmake(userweap & whip);
void mindshotmake(userweap & mindshot);
void weapons(userweap & regcannon);
//MAIN FUNCTION
int main(){
nav dummynav;
userweap dummyweap;
intro();
while( choice(dummynav) == "no") cout<< "let me show you them again" << endl;
btutorial();
while( allgood() == "no");
sleep(1);
weapons(dummyweap);
return 0;
}
void intro()
{
cout<<"what's your name again?"<<endl;
cin>>charname;
cout<<"hello, "<<charname<<endl;
sleep(2);
cout<<"welcome to the pentagon"<<endl;
sleep(1);
cout<<"year: 2050 C.E." << endl;
sleep(1);
cout<<"you have been nominated to control a 'navigator.' a digital fighting warrior that we use to fight the rebellion."<<endl;
sleep(1);
cout<<"..."<<endl;
sleep(1);
cout<<"what's this? you've never heard of the rebellion before?"<<endl;
sleep(1);
cout<<"the year was 2015, when the citizens of the united states of america rose up against the government. little did they know, the government had installed a top-secret project called 'SEVEN' (or s.ecretly e.liminate v.iolent e.lectronic n.ews) into the world wide web . they used this successfully to break up riots by intercepting internet-bound messages. soon after, the rebellion's top scientists figured out the programs weak-spots, and created viruses 'bugs' to destroy said program."<<endl;
sleep(12);
cout<<"that's where you come in." << endl;
sleep(1);
cout<<"we've created an elite squad of web 'navigators' to destroy any viruses we encounter." <<endl;
sleep(1);
cout<<"you will be tasked with controlling one of these 'navigators' to end the rebellion once and for all."<<endl;
sleep(1);
cout<<"let me show you them"<<endl;
}
string choice(nav & user)
{
sleep(1);
cout<<"..."<<endl;
sleep(1);
cout<<"bladenav: a well balanced navigator that can wield a multitude of de-bugging weapons"<<endl;
cout<<"arrownav: a speed-oriented navigator that can attack stealthily from far range"<<endl;
cout<<"fistsnav: an attack-heavy navigator with low speed but with a variety of high-power weapons"<<endl;
cout<<"shellnav: an extremely high defense navigator with low speed and attack "<<endl;
cout<<"golemnav: a high attack, high defense navigator with low speed, and a narrow range of attacks "<< endl;
cout<<"flamenav: a navigator with flame-based attacks with relatively large luck"<<endl;
cout<<"aqueonav: a navigator with aqua-based attacks with relatively large luck"<<endl;
cout<<"plantnav: a navigator with plant-based attacks with relatively large luck"<<endl;
cout<<"psychnav: a high hit points navigator with indirect attacking abilities"<<endl;
cout<<"chicknav: a navigator with terrible stats all around and no redeeming qualities whatsoever except for it's 100% luck stat."<<endl;
sleep(3);
cout<<"you may choose one and only one to control. pick wisely"<<endl;
string navchoice;
cin>> navchoice;
if (navchoice == "bladenav") user.INIT(50, 50, 50, 100, 10, 40);
else if (navchoice =="arrownav") user.INIT(55, 30, 65, 100, 10, 40);
else if (navchoice =="fistsnav") user.INIT(80, 55, 10, 115, 10, 30);
else if (navchoice =="shellnav") user.INIT(25, 80, 25, 120, 10, 40);
else if (navchoice =="golemnav") user.INIT(70, 70, 10, 100, 10, 40);
else if (navchoice =="flamenav") user.INIT(50, 50, 40, 100, 15, 45);
else if (navchoice =="aqueonav") user.INIT(50, 50, 40, 100, 15, 45);
else if (navchoice =="plantnav") user.INIT(50, 50, 40, 100, 15, 45);
else if (navchoice =="psychnav") user.INIT(20, 30, 30, 150, 10, 50);
else if (navchoice =="chicknav") user.INIT(20, 20, 20, 100, 100, 40);
sleep(1);
user.display();
sleep(1);
cout<<"is this you're final selection, "<< charname <<"?"<<endl;
string yesno;
cin >> yesno;
return yesno;
}
void btutorial()
{
userweap dummyweap;
regcannonmake(dummyweap);
beamswordmake(dummyweap);
laserbowmake(dummyweap);
flyingfistmake(dummyweap);
razordiscmake(dummyweap);
blockpunchmake(dummyweap);
flaremake(dummyweap);
wavemake(dummyweap);
whipmake(dummyweap);
mindshotmake(dummyweap);
cout<<"now that you've chosen your navigator, you should probably learn how to use it."<<endl;
sleep(1);
cout<<"..."<<endl;
sleep(1);
cout<<"when you find an enemy 'bug', battle mode is initiated."<<endl;
sleep(1);
cout<<" ______________________________________________________"<<endl;
cout<<"|ATTACK| WEAPONS | ITEMS | FLEE | CHECK STATS | DEFEND |"<<endl;
cout<<"|______|_________|_______|______|_____________|________|"<<endl;
sleep(1);
cout<<"then you select an option. choosing 'attack' attacks the opponent. The damage you deal is equal to the weapon's power added to your attack, then subtracted by the enemy's defense. 'weapons' accesses your de-bugging arsenal, 'items' allows you to view and use your one-use items, 'check stats' checks both your and the enemy's health points and stats, and 'defend' doubles your defense for the next turn, but takes up a turn. activating 'flee' will end the encounter unless your speed is lower than the 'bug's'."<<endl;
sleep(10);
cout<<"then, after your turn is over, your oppenent will either attack or defend."<< endl;
}
string allgood()
{
sleep(5);
cout<< "all good?" <<endl;
string yesno;
cin >> yesno;
return yesno;
}
//WEAPON CREATION
void regcannonmake(userweap & regcannon)
{
regcannon.INIT(1,5,0);
}
void beamswordmake(userweap & beamsword)
{
beamsword.INIT(2,7,1);
}
void laserbowmake(userweap & laserbow)
{
laserbow.INIT(3,6,1);
}
void flyingfistmake(userweap & flyingfist)
{
flyingfist.INIT(4,8,1);
}
void razordiscmake(userweap & razordisc)
{
razordisc.INIT(5,4,1);
}
void blockpunchmake(userweap & blockpunch)
{
blockpunch.INIT(6,5,1);
}
void flaremake(userweap & flare)
{
flare.INIT(7,5,1);
}
void wavemake(userweap & wave)
{
wave.INIT(8,5,1);
}
void whipmake(userweap & whip)
{
whip.INIT(9,5,1);
}
void mindshotmake(userweap & mindshot)
{
mindshot.INIT(10,7,1);
}
//CHECKSTATS
void checkstats(bug & simbug, nav & user)
{
cout<<"your stats:"<< endl;
user.display();
cout<<""<< endl;
cout<<"bug's stats:"<< endl;
simbug.display();
}
void weapons(userweap & regcannon)
{
sleep(1);
cout<<"weapons:"<<endl;
cout<<"regcannon"<<endl;
sleep(1);
cout<< "pick one" <<endl;
string weapchoice;
cin >> weapchoice;
if (weapchoice == "regcannon")
{
regcannon.display();
regcannon.equip();
while( allgood() == "no");
cout<< charname << " equipped the regcannon" <<endl;
}
}
void flee();
void flee()
{
}
void defend(nav & user);
void defend(nav & user)
{
}
void items();
void items()
{
}
bool battle(nav & user);
bool battle(nav & user)
{
bug simbug;
simbug.INIT(10,10,10,10,10,10);
cout<<simbug.bugatk<<endl;
return true;
}
答案 0 :(得分:3)
随着您的代码开始像这样构建,错过细微差别变得更容易也更容易。在这种情况下,你已经与两组变量的问题发生了冲突,这两组变量意味着同样的事情,但不能区分谁在特定操作的哪一侧。
在userweap::INIT
函数中,您将收到两个值来初始化对象,weappower和weaponcost。但是,不是初始化它们,而是将对象中的数据复制到参数中,然后返回。
void userweap::INIT(int id, int weappower /*input*/, int weapcost /*input*/)
{
weappower /*input*/ = power /*member value*/;
weapcost /*input*/ = cost /*member value*/;
}
这与你打算做的完全相反。因此,您的会员值power
和cost
永远不会故意分配值,而您的32767是来自“未定义行为”的随机数据。
帮助自己避免这种情况:给成员变量一个前缀,比如'm_'或'm'
int m_power; // member variable that stores power.
现在,您的代码将对您正在做的事情进行一些额外的指示:
void userweap::INIT(int id, int weappower, int weapcost)
{
weappower = m_power;
weapcost = m_cost;
}
加上你可以在这里避免'weapon'前缀:
void userweap::INIT(int id, int power, int cost)
{
power = m_power;
cost = m_cost;
}
更容易看到错误,正确的代码是:
void userweap::INIT(int id, int power, int cost)
{
m_power = power;
m_cost = cost;
}
答案 1 :(得分:2)
您设置的参数不是您的成员,因此power
未初始化。
void userweap::INIT(int id, int weappower, int weapcost)
{
weappower = power;
weapcost = cost;
}
应该是:
void userweap::INIT(int id, int weappower, int weapcost)
{
power = weappower;
cost = weapcost;
}
另一件事:使用构造函数!
userweap::userweap(int id, int weappower, int weapcost)
: power(weappower),
cost(weapcost)
{
}
下次请到达目的地(不要发布“代码墙”)
答案 2 :(得分:1)
更改行:
weappower = power;
weapcost = cost;
他们应该是:
power = weappower;
cost = weapcost;
现在看来,你永远不会分配给power
,所以它是未定义的。
答案 3 :(得分:1)
void userweap::INIT(int id, int weappower, int weapcost)
{
weappower = power;
weapcost = cost;
}
void userweap::display()
{
cout<<"power: " << power <<endl;
}
在INIT()中,您将成员变量的值赋给参数。它需要反过来。