我的程序运行时出错(运行时检查失败#2 - 堆栈变量' userInput'已损坏。)

时间:2018-05-10 18:58:30

标签: c++ error-handling runtime-error runtime

如果我在错误的部分发帖,我很抱歉。

当我运行程序时,我收到错误(运行时检查失败#2 - 堆栈变量' userInput'已损坏。)我不知道我为此做了什么跑步。 (对不起我的英语我是法国人)。如果你可以帮助我,那将是值得欣赏的。我试图通过大量的调试器(Visual studio,gdb,gdb online,..)运行它,但我仍然无法找到原因。如果你也可以查看我的代码。像你发现的大错误一样会很感激。谢谢:))

#include "stdafx.h"
#include <cstdlib>
#include <string>
#include <iostream>
#include <time.h>

// START FUNCTION DECLARATION 

void setupGame(int gameBoard[4][19]);
void showGrid(int gameBoard[4][19]);
void calulateGame(std::string userInput);

// END FUNCTION DECLARATION 

int main(int argc, char** argv) {

    // Basic game variable
    std::string userInput = "";
    bool bGameLoop = true;
    int gameBoard[4][19];

    // Setup the game
    setupGame(gameBoard);

    do {
        // Reloading the scene
        //system("clear");

        // Show/Update the game grid
        showGrid(gameBoard);

        // Wait for user input
        /*userInput = "";
        std::cin >> userInput;
        if (userInput != "w" || userInput != "a" ||
        userInput != "s" || userInput != "d")
        {
            std::cout << "Please use [W][A][S][D] to move <3";
        }*/

    } while (bGameLoop);

    return 0;
}


// START FUNCTION  

/**
* Calculate all in the game (if user died, cannot go, win, etc)
* @param userInput The last input
*/
/*void calulateGame(std::string userInput) {


}*/

/**
* Show the grid.
*/
void showGrid(int gameBoard[4][19]) {

    //# -------------------- #                                              
    //|O.................... |                                           
    //# -------------------- # 

    // Show top bar
    std::cout << "DUNGEON CRAWL V1\n\n";

    std::cout << "#--------------------#\n";
    std::string cToOut;
    for (int y = 0; y <= 4; y++) {
        std::cout << "|";
        for (int x = 0; x <= 19; x++) {
            switch (gameBoard[y][x]) {
                case 0:
                    cToOut = " ";
                    break;
                case 1:
                    cToOut = "@";
                    break;
                case 2:
                    cToOut = "X";
                    break;
                case 3:
                    cToOut = "O";
                    break;
                default:
                    cToOut = " ";
                    break;
                };

            std::cout << cToOut;
        }
        std::cout << "|\n";
    }

    std::cout << "#--------------------#\n";
}

/**
* Create the game array and setup trap and the player.
*/
void setupGame(int gameBoard[4][19]) {
    // 0 = empty data
    // 1 = player 
    // 2 = exit
    // 3 = trap

    // Populate array with zero
    for (int y = 0; y <= 4; y++) {
        for (int x = 0; x <= 19; x++) {
            gameBoard[y][x] = 0;
        }
    }

    // Setup player & exit
    gameBoard[0][0] = 1;
    gameBoard[4][19] = 2;

    // Setup random trap 
    int trapX, trapY;
    bool bAlreadyUse = false;
    srand(time(NULL));
    for (int t = 1; t <= 5; t++) {
        do {
            trapX = rand() % 19;
            trapY = rand() % 4;

            if (gameBoard[trapY][trapX] == 3) {
                bAlreadyUse = true;
            }
            else {
                bAlreadyUse = false;
            }

            // Dont use spawn or end pos
        } while ((trapX == 0 && trapY == 0) || (trapX == 19 && trapY == 4)
            || bAlreadyUse);

        gameBoard[trapY][trapX] = 3;
    }
}

// END FUNCTION  

我忘了提及我是c ++的新手。这是一个尝试的游戏:)

0 个答案:

没有答案