Question

I am creating a simple code breaker game for my C++ class.

It works perfectly when I run the game once, but when I run it a second time ( ie: give the user to play again in the same instance) I get Floating point exception (core dumped).

It occurs when the user Choose choice 1 twice in one instance.

Here is my code:

#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;
}
Was it helpful?

Solution

Floating point exception occurs when x%0 happens.

In your code it must be concurring here:

roll(){
    int val = rand() % slotNumber;
    slotNumber = val++;
}

Case: if rand() generates a multiple of 5
val becomes 0 and slotNumber gets set to 0, next time roll() is called it crashes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top