代码之家  ›  专栏  ›  技术社区  ›  Lions654321

我的程序出现abort()错误,但我无法调试它,因为它是程序的加载部分?

  •  1
  • Lions654321  · 技术社区  · 9 年前

    每次我启动这个应用程序时,它都会进入一个完美的菜单,但当我点击start()后,它就会中止,我有一种理论认为这是stoi,但我不确定。正如你所看到的,我正在做一个21点游戏,它使用数组然后rand()来随机化出数组,但它必须将数组中的字符串转换为整数。但你必须担心国王或数组中的0,所以我必须解释这一点。

    // Blackjack.cpp 
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <Windows.h>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    int start(); // forward decleration
    
    int main() {
        int menu = 0;
        SetConsoleTitle(TEXT("BlackJack By Paul V 1.0"));
        cout << "Welcome to BlackJack!" << endl;
        cout << "1. Start the game!!" << endl;
        cout << "2. Exit" << endl;
        cout << "ENTER HERE:" << flush;
        cin >> menu;
        if (menu == 1) {
            start();
        }
        if (menu == 0) {
            cout << "Program Ending....." << endl;
        }
        return 0;
    }
    int start() {
        SetConsoleTitle(TEXT("LOADING....."));
        srand((unsigned)time(NULL));
        int menu = 0;
        int yo1 = rand() % 13;
        int yo2 = rand() % 13;
        int yo3 = rand() % 13;
        int yo4 = rand() % 13;
        int yo5 = rand() % 13;
        int yo6 = rand() % 4;
        int yo7 = rand() % 4;
        int yo8 = rand() % 4;
        int yo9 = rand() % 4;
        int yo = rand() % 4;
        int card1 = 0;
        int card2 = 0;
        int card3 = 0;
        int card4 = 0;
        int card5 = 0;
        string card_names[13] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
        string card_types[4] = { "Diamonds", "Spades", "Clubs", "Hearts" };
        string goody = card_names[yo1];
        string goddy2 = card_names[yo2];
        string goddy3 = card_names[yo3];
        string goddy4 = card_names[yo4];
        string goddy5 = card_names[yo5];
        if (yo1 == 10 || yo1 == 11 || yo1 == 12) {
            const int card1 = 10;
        }
        if (yo2 == 10 || yo2 == 11 || yo2 == 12) {
            const int card2 = 10;
        }
        if (yo3 == 10 || yo3 == 11 || yo3 == 12) {
            const int card3 = 10;
        }
        if (yo4 == 10 || yo4 == 11 || yo4 == 12) {
            const int card4 = 10;
        }
        if (yo5 == 10 || yo5 == 11 || yo5 == 12) {
            const int card5 = 10;
        }
        if (yo1 == 0) {
            const int card1 = 1;
        }
        if (yo2 == 0) {
            const int card2 = 1;
        }
        if (yo3 == 0) {
            const int card3 = 1;
        }
        if (yo4 == 0) {
            const int card4 = 1;
        }
        if (yo5 == 0) {
            const int card5 = 1;
        }
        else {
            int card1 = stoi(card_names[yo1]);
            int card2 = stoi(card_names[yo2]);
            int card3 = stoi(card_names[yo3]);
            int card4 = stoi(card_names[yo4]);
            int card5 = stoi(card_names[yo5]);
        }
        SetConsoleTitle(TEXT("BlackJack By Paul V 1.0"));
        cout << "Your starting card is a " << goody << " of " << card_types[yo6] << endl;
        return 0;
    }
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Cypher    9 年前

    问题在于您的stoi()。这是因为yo'n的值会一直延续到代码的最后一个“else”部分,即使它们超出了1到9的范围。

    在将所有yo'n变量作为card_names[yo'n']传递给stoi之前,需要对它们进行范围检查。

    正如smead所说,请编写更干净的代码。太多的“if”使得它几乎无法读取和调试。

        2
  •  1
  •   smead    9 年前

    stoi() see documentation ). 您的阵列 card_names 包含多个非整数字符串(“Ace”、“Jack”等)。

    我甚至不知道你想做什么:你的价值观 yo1 通过 yo5 已经包含了卡号,那么为什么需要将其转换为字符串并返回?

    顺便说一句,请使用数组!看到这样的事情会让我眼睛痛 年1月 通过 yo9 card1 通过 card5