簡體   English   中英

有人可以幫我弄清楚為什么我的代碼不能正常工作嗎?

[英]Can someone help me figure out why my code isn't working properly?

#include <iostream>
using namespace std;

int fight();
int lowblow();
int eyes();

int run();
int underground();
int officebuilding();
int main()
{
    int choice1;
    int fchoice2, fchoice3;
    int rchoice2, rchoice3;
    cout << "As you’re walking down the street you feel somebody grab your shoulder, when you turn around to see. You notice the person is holding a knife towards you and demanding for your valuables." << endl; 
    cout << "Do you fight or run?" << endl;
    cout << "1 - Fight" << endl;
    cout << "2 - Run" << endl;
    cin >> choice1;
    while (choice1 != 1 && choice1 != 2) {
        cout << "Do you fight or run? " << endl;
        cout << "1 - Fight" << endl;
        cout << "2 - Run" << endl;
        cin >> choice1;
    }

    if (choice1 == 1) {
        fchoice2 = fight();
        while (fchoice2 != 1 && fchoice2 != 2) {
            fchoice2 = fight();
        }
        if (fchoice2 == 1) {
            fchoice3 = lowblow();
            while (fchoice3 != 1 && fchoice3 != 2) {
                fchoice3 = lowblow();
            }
        }
        if (fchoice2 == 2) {
            fchoice3 = eyes();
            while (fchoice3 != 1 && fchoice3 != 2) {
                fchoice3 = eyes();
            }

            if (choice1 == 2) {
                rchoice2 = run();
                while (rchoice2 != 1 && rchoice2 != 2) {
                    rchoice2 = run();
                }
                if (rchoice2 == 1) {
                    rchoice3 = underground();
                    while (rchoice3 != 1 && rchoice3 != 2) {
                        rchoice3 = underground();
                    }
                    if (rchoice3 == 1) {
                        officebuilding();
                    }
                }
            }
        }
    }
}

int fight()
{
    system("clear");
    int c2;
    cout << "You choose to fight off the attacker and suddenly all those self-defense classes come rushing into your mind.As you fight to defend yourself, you find yourself getting outmatched and have to try something drastic to survive." << endl; 
    cout << "1 - Go for the low blow" << endl;
    cout << "2 - Go for the eyes " << endl;
    cin >> c2;
    return c2;
}
int lowblow()
{
    system("clear");

    cout << "You catch your attacker off guard and land the hit, to your surprise the attack proves ineffective.In retaliation you get hit so hard that you start seeing stars and soon blackout leaves you defenseless.Now you are knocked out and getting your valuables taken." < endl;   
                                                                             
    cout << "The End" << endl;
    return 0;
}

int eyes()
{
    system("clear");

    cout << "Using the element of surprise to try fighting back gave you no advantage, so you resort to another tactic. You’ve seen it in movies and it always worked out there. You decide to go for the eyes and to your surprise it works. You use this opportunity to escape and get to safety, all while keeping your valuables safe. "<< endl;
    cout << "The End" << endl;
    return 0;
}
int run()

{
    system("clear");
    int c2;
    cout << " While running down the street to loose the attacker, you noticed that there was an underground subway a little down the road.Immediately to your left there is an office building you could run into and try to lose your attacker there."<< endl; 
    cout << "1 - Underground ?" << endl;
    cout << "2- office building?" << endl;
    cin >> c2;
    return c2;
}

int underground()
{
    system("clear");

    cout << "As you make your way towards the underground subway station you notice rushing out.In a desperate attempt to reach safety,        you run down the stairs while narrowly avoiding others.Upon arriving at the station you look back and notice that you lost your attacker.You are safe now.";            
    cout << "The End" << endl;
    return 0;
}
int officebuilding()
{
    system("clear");
    cout << "A spontaneous decision leads you into an office building, while it may look safe, upon entering you notice everything is silent as the building seems to be completely empty.None of it matters however, since you have to escape at any cost in order.While trying to find a good way to lose the attacker you hear footsteps behind you.The attacker has cornered you and has taken your valuables." << endl; 
    cout << "The End" << endl;
    return 0;
}

我想我正在寫一個分支路徑的故事。 我不確定為什么在我選擇一個選項后它開始閃爍,而對於另一個選項它甚至不起作用。 我不確定在哪里修復代碼中的這些錯誤。 我不確定我是否在函數或 while 循環的某個地方搞砸了。 我試過改變一些東西,但似乎沒有任何效果。

您正在等待rchoice3為 1 或 2 的 while 循環中調用underground() ,但underground()始終返回 0,因此rchoice3始終為 0..

while (rchoice3 != 1 && rchoice3 != 2) {
   rchoice3 = underground();
}

我認為您需要更改此塊:

                if (rchoice2 == 1) {
                    rchoice3 = underground();
                    while (rchoice3 != 1 && rchoice3 != 2) {
                        rchoice3 = underground();
                    }
                    if (rchoice3 == 1) {
                        officebuilding();
                    }
                }

至:

                if (rchoice2 == 1) {
                    underground();
                } else {
                    officebuilding();
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM