2013-11-26 72 views
-3

幫助,每當我運行我的遊戲,每當我調用武器的display()函數時,它總是以32767的形式返回!我該如何做到這一點,以便在調用顯示功能時,將該武器電源打印到控制檯上?提前致謝!C++ 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; 
} 
+2

請把這個減少到最小的例子來說明你的問題。 – ChrisInEdmonton

回答

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*/; 
} 

這和你打算做的事完全相反。因此,您的成員值powercost從來沒有有意分配的值,您的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; 
} 

再加上你將能夠避免「WEAP」字頭的位置:

void userweap::INIT(int id, int power, int cost) 
{ 
    power = m_power; 
    cost = m_cost; 
} 

更容易看到霧ake,正確的代碼是:

void userweap::INIT(int id, int power, int cost) 
{ 
    m_power = power; 
    m_cost = cost; 
} 
1

更改線路:

weappower = power; 
weapcost = cost; 

他們應該是:

power = weappower; 
cost = weapcost; 

目前的情況是,你永遠不分配給power,所以這是不確定的。

1
void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    weappower = power; 
    weapcost = cost; 
} 
void userweap::display() 
{ 
    cout<<"power: " << power <<endl; 

} 

在您的INIT()中,您將成員變量的值分配給參數。它需要是相反的。

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) 
{ 
} 

而接下來的時間請去點(不要發佈「代碼牆」)