找不到核心转储错误C++的原因

Cannot Find cause of Core Dump Error C++

本文关键字:C++ 错误 核心 转储 找不到      更新时间:2023-10-16

我正在为我的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()时,它会崩溃