我正在为我的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 5\n";
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;
}
答案 0 :(得分:1)
x%0
发生时发生浮点异常。
在您的代码中,必须同意:
roll(){
int val = rand() % slotNumber;
slotNumber = val++;
}
案例:如果rand()
生成5的倍数
val变为0并且slotNumber设置为0,下次调用roll()
时崩溃