找不到核心转储错误C++的原因
Cannot Find cause of Core Dump Error C++
我正在为我的C++类创建一个简单的代码破解游戏。
当我运行一次游戏时,它运行得很好,但当我第二次运行时(即:让用户在同一实例中再次玩),我会得到Floating point exception (core dumped)
。
当用户在一个实例中两次选择选项1时,就会发生这种情况。
这是我的代码:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Dice {
public:
Dice();
void roll();
int getNumber();
void setCode();
bool getAttempt();
void setAttempt();
int slotNumber;
private:
bool attempt = false;
};
class Menu {
public:
void printMenu();
int userChoice();
int getChoice();
void printInstructions();
string userAttempt();
private:
int choice;
string guess;
};
Dice::Dice(){
//Set Default Vaules
slotNumber = 5;
srand(time(NULL));
}
void Dice::roll(){
int val = rand() % slotNumber;
slotNumber = val++;
}
int Dice::getNumber(){ return slotNumber; }
bool Dice::getAttempt(){ return attempt; }
void Dice::setAttempt(){ attempt = true; }
void Menu::printMenu(){
cout << "===========Code Breaker==============" << endl;
cout << "1. Play Game " << endl;
cout << "2. Instructions " << endl;
cout << "3. Quit " << endl;
cout << "=====================================" << endl;
}
int Menu::userChoice(){
cout << "Please input your choice now (1-3): ";
cin >> choice;
while(choice < 1 || choice > 3) {
cout << "Invalid Option. Please try again." << endl;
cout << "Please input your choice now (1-3): ";
cin >> choice;
}
}
int Menu::getChoice() { return choice; }
void Menu::printInstructions() {
cout << "==========Intructions================" << endl;
cout << "1. Input: You MUST use a continuous string. ie: 12345 Do not use spaces. ie: 1 2 3 4 5n";
cout << "2. The code shall be represented by five astricks (*****) When you guess correctly, the astrick shall be replaced with the number (1****)n";
cout << "3. The code shall be a random whole number from 0-5.n";
cout << "=====================================" << endl;
}
string Menu::userAttempt() {
cout << "nInput Guess now: ";
cin >> guess;
while(guess.size() < 1 || guess.size() > 5) {
cout << "Invalid Input. Please try again." << endl;
cout << "Input guess now: ";
cin >> guess;
}
return guess;
}
int main() {
//Variables
string guess;
//Objects
Menu menu;
Dice dice[5];
//Menu
do {
menu.printMenu();
menu.userChoice();
if (menu.getChoice() == 1) {
for (int i = 0; i<5; i++){
dice[i].roll();
//cout << dice[i].slotNumber;
}
cout << "CODE: *****";
do {
guess = menu.userAttempt();
for( int i = 0; i < 5; i++) {
if((guess[i] - 48) == dice[i].slotNumber) { dice[i].setAttempt(); }
//if(dice[i].getAttempt() == false) { cout << 1;}
//else {cout << 0;}
}
cout << "CODE: ";
for(int i = 0; i < 5; i++){
if(dice[i].getAttempt() == false) {
cout << "*";
} else {
cout << dice[i].slotNumber;
}
}
} while(dice[0].getAttempt() == false || dice[1].getAttempt() == false || dice[2].getAttempt() == false || dice[3].getAttempt() == false || dice[4].getAttempt() == false);
cout << endl;
cout << "congratulations! You won!" << endl;
}else if(menu.getChoice() == 2) {
menu.printInstructions();
}
} while(menu.getChoice() != 3);
return 0;
}
发生x%0
时发生浮点异常。
在你的代码中,它必须在这里一致:
roll(){
int val = rand() % slotNumber;
slotNumber = val++;
}
案例:如果rand()
生成5的倍数
val变为0,slotNumber被设置为0,下次调用roll()
时,它会崩溃
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'