为什么我的C ++控制台突然连续输出

时间:2018-11-18 23:35:59

标签: c++

我正在构建一个基于控制台,基于文本的地牢游戏,到目前为止,该游戏进展顺利。我试图在某种“开发人员模式”中添加一个入口,然后在我的room1()函数开始运行后,它将连续输出我的文本到控制台。在添加此功能之前,它运行良好,因此我取出了放入程序中的代码,但仍然会发生。它还完全跳过了getline(cin,playerMove),并且没有为您提供输入字符串的选项。我也知道这可能是非常糟糕的编码策略,但是我只参加了不到三个星期,尽管欢迎任何提示,因为这有望成为我的职业。

提前谢谢!

代码:

#include <iostream>
#include <windows.h>
#include <locale>
#include <fstream>
#include <string>

using namespace std;

//Declaring Handles
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

//Declaring Functions
void startText(), room1(), room2(), room3(), room4(), playerMoveQ(), invalidAnswer(), playerMoveOptions(), gameEngine(), healthBar(), root();

//Declaring Strings
string enterCave, playerName, bag = "Currently, your bag is empty.", playerMove, getRoot;

//Declaring Integers
int cash = 0;
int playerLocation = 1;

//Declaring Booleans
bool gameOn = true, firstTurn = true, swordObtained = false, armorObtained = false, bagFilled = false;

void gameEngine()
{
    room1();
}

void startText()
{
    SetConsoleTextAttribute(hConsole, 14);
    cout << "Enter your name : ";
    getline(cin, playerName);
    endl(cout);

    SetConsoleTextAttribute(hConsole, 1);
    cout << "################################" << endl;
    cout << "#                              #" << endl;
    cout << "#          Dr. Snidley         #" << endl;
    cout << "#                              #" << endl;
    cout << "################################" << endl;
    endl(cout);

    SetConsoleTextAttribute(hConsole, 13);
    cout << "The Princess of the Kingdom, Percia, has been kidnapped by the evil Dr. Snidley." << endl;
    cout << "It is up to you, brave traveler, to enter this evil dungeon and save the Princess of Percia," << endl << "and bring peace to her Kingdom. ";
    cout << "There is 9 rooms in this dungeon and one exit," << endl << "there is only ever one monster and/or items in a room.";
    cout << "Enter the cave now, or cower away in disgrace!" << endl;
    endl(cout);
}

void room1()
{
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 1. As you look gameEngine();around you notice a series of canvas' around the room." << endl;
        cout << "You see that all of the pictures are torn out, all but the Princess'. You know you don't have time and you must hurry!" << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your South. There is a room to your East." << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl;
        cout << bag;
        endl(cout);

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
}

void room2()
{
    SetConsoleTextAttribute(hConsole, 8);
    cout << "Currently you are in Room 2. This room is shockingly lighten up, at least more than the others." << endl << "You hear an abundance of growls coming from the room to your South." << endl;

    SetConsoleTextAttribute(hConsole, 11);
    cout << "There is a room to your East. There is a room to your South. There is a room to your West." << endl;

    healthBar();

    SetConsoleTextAttribute(hConsole, 2);
    cout << "Your bag contains the following items :" << endl << bag << endl;

    SetConsoleTextAttribute(hConsole, 1);
    cout << "Current cash is : " << cash << endl;
    endl(cout);

    playerMoveQ();
}

void room3()
{
    if (armorObtained == false)
    {
        //Story / Room Loacation
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 3. In this room you see a shimmering coming from the corner." << endl << "You decide to check it out and find a rugged looking set of armor." << endl;
        //Saying Rooms
        SetConsoleTextAttribute(hConsole, 11);gameEngine();
        cout << "There is a room to your South. There is a room to your West." << endl;
        //Items on floor
        SetConsoleTextAttribute(hConsole, 15);
        cout << "The ARMOR is in its stand on the other side of the room." << endl;
        //Health Bar
        healthBar();
        //Items in bag
        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;
        //Total cash
        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);
        //Ask players next move
        playerMoveQ();
    } else if (armorObtained == true)
    {
        //Story / Room Loacation
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 3. In this room you see a shimmering coming from the corner." << endl << "You decide to check it out and find a rugged looking set of armor." << endl;
        //Saying Rooms
        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your South. There is a room to your West." << endl;
        //Displaying player health
        healthBar();
        //Items in bag
        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;
        //Total cash
        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);
        //Ask players next move
        playerMoveQ();
    }
}

void room4()
{
    if (swordObtained == false)
    {
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 4. This room looks nothing like the last, nothing but a single podium is in the center of the room. Although something does not feel right." << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your North. " << endl;

        SetConsoleTextAttribute(hConsole, 15);
        cout << "The SWORD is lying at the feet of the podium." << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
    } else if (swordObtained == true)
    {
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 4. This room looks nothing like the last, nothing but a single podium is in the center of the room. Although something does not feel right." << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your North. " << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
    }
}

void room5()
{
    if (armorObtained == false)
    {
        //Story / Room Location
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are Room 5. As you enter it is hard to divert your eyes from the Cyberus laying by your feet. " << endl << "You look to see the other side of the room and see a locked door. You thne see the Key around the Cyberus' neck.";
        //Outputing Rooms
        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your East. There is a ";
        SetConsoleTextAttribute(hConsole, 8);
        cout << "LOCKED";
        SetConsoleTextAttribute(hConsole, 11);
        cout << "to your South.";gameEngine();
    }
}

void playerMoveQ()
{
    SetConsoleTextAttribute(hConsole, 14);
    cout << "What would you like to do : ";
    getline(cin, playerMove);
    if (firstTurn == true)
    {
        getline(cin, playerMove);
    }
    firstTurn = false;
    playerMoveOptions();
}

void playerMoveOptions()
{
    if (playerMove == "move south" && playerLocation == 1)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 4" << endl;
        playerLocation = 4;
        room4();
    } else if (playerMove == "pick sword" && playerLocation == 4)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 11);
        cout << "You have successfully picked up the SWORD!" << endl;
        endl(cout);
        if (bagFilled == true)
        {
            bag += "\nSWORD";
        } else if (bagFilled == false)
        {
            bag = "SWORD";
        }
        cash += 500;
        swordObtained = true;
        bagFilled = true;
        room4();
    } else if (playerMove == "move north" && playerLocation == 4)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 1" << endl;
        playerLocation = 1;
        room1();
    } else if (playerMove == "move east" && playerLocation == 1)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 2" << endl;
        playerLocation = 2;
        room2();
    } else if (playerMove == "move east" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 3" << endl;
        playerLocation = 3;
        room3();
    } else if (playerMove == "move west" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 1" << endl;
        playerLocation = 1;
        room1();
    } else if (playerMove == "move west" && playerLocation == 3)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 2" << endl;
        playerLocation = 2;
        room2();
    } else if (playerMove == "pick armor" && playerLocation == 3)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 11);
        cout << "You have successfully equiped the ARMOR! You may have 1 extra hit now!" << endl;
        endl(cout);
        if (bagFilled == true)
        {
            bag += "\nARMOR";
        } else if (bagFilled == false)
        {
            bag = "ARMOR";
        }
        cash += 1000;
        armorObtained = true;
        bagFilled = true;
        room3();
    } else if (playerMove == "move south" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully move to Room : 5" << endl;
        playerLocation = 5;
        room5();
    } else
    {
        invalidAnswer();
    }
}

void invalidAnswer()
{
    SetConsoleTextAttribute(hConsole, 12);
    endl(cout);
    cout << "INVALID COMMAND. Please enter a valid command!" << endl;
    playerMoveQ();
}

void healthBar()
{
    if (armorObtained == false)
    {
        SetConsoleTextAttribute(hConsole, 15);
        cout << "[";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";gameEngine();
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "]" << endl;
    } else if (armorObtained == true)
    {
        SetConsoleTextAttribute(hConsole, 15);
        cout << "[";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "]" << endl;
    }
}

void root()
{
    system("CLS");
    getline(cin, getRoot);
    if (getRoot == "room 1")
    {
        room1();
    } else if (getRoot == "room 2")
    {
        room2();
    } else if (getRoot == "room 3")
    {
        room3();
    } else if (getRoot == "room 4")
    {
        room4();
    } else if (getRoot == "room 5")
    {
        room5();
    }
}

int main()
{
    //Story & Start Text
    startText();
    //Game Engine
    if (playerName == "sudo root")
    {
        root();
    } else 
    {
        gameEngine();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

我建议您学习如何调试。在调试器中运行代码,并设置断点,以使您怀疑代码转错了方向。

由于您正在编写Windows应用程序,因此我假设您正在使用Visual Studio。按F5键在调试器中启动程序。关于如何调试有很多教程。签出来。