我的程序得到一个abort()错误,但我不能调试它,因为它是程序的一个加载部分
My program gets an abort() error yet i cant debug it due to it being a loading part of the program?
每次我启动这个应用程序,它去工作完美的菜单,但在我点击开始()之后,它只是中止,我有一个理论,它是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"等)。
我甚至不确定你在那里想做什么:你的值yo1
到yo5
已经包含了卡号,那么为什么你需要将其转换为字符串并返回?
顺便说一下,请使用数组!看到yo1
到yo9
和card1
到card5
这样的东西,我的眼睛很痛
相关文章:
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用另一个程序调用正在运行的 C++ 程序中的函数
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 编写一个程序,帮助Tudio将MIDI音符转换为名称和八度音阶
- 有没有办法在一个C++程序中同时使用 FFTW 和 cuFFTW?
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- C++将结构复制到另一个程序的资源
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 仅在 boost::hash_combine 中运行一个程序期间保证确定性
- 如何制作一个程序,它使用特定单词加密和解密消息?
- 尝试创建一个程序来查找非二叉树的高度.最终得到一个很长的循环,没有答案
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 如何编写一个程序来打印特定基数的前N个数字
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?