我的程序得到一个abort()错误,但我不能调试它,因为它是程序的一个加载部分

My program gets an abort() error yet i cant debug it due to it being a loading part of the program?

本文关键字:一个 程序 因为 加载部 调试 错误 abort 我的 不能      更新时间:2023-10-16

每次我启动这个应用程序,它去工作完美的菜单,但在我点击开始()之后,它只是中止,我有一个理论,它是stoi,但我不确定。正如你所看到的,我正在制作一个21点游戏,它使用数组然后rand()从数组中随机化,然后它必须将数组中的字符串转换为整数。但是你还得考虑k或者数组中的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;
}

问题在于您的stoi()。这是因为你的值不会一直跟随到代码的最后一个"else"部分,即使它们超出了1到9的范围。

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

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

stoi()将字符串转换为数字,但前提是您的字符串可以有效地转换为数字(参见文档)。你的数组card_names包含几个非整数字符串("Ace", "Jack"等)。

我甚至不确定你在那里想做什么:你的值yo1yo5已经包含了卡号,那么为什么你需要将其转换为字符串并返回?

顺便说一下,请使用数组!看到yo1yo9card1card5这样的东西,我的眼睛很痛

相关文章: