2014-04-21 2 views
1

저는 C++ 클래스 용 간단한 코드 브레이커 게임을 만들고 있습니다.코어 덤프 오류의 원인을 찾을 수 없습니다. C++

게임을 한 번 실행하면 완벽하게 작동하지만 두 번째로 실행하면 (예 : 동일한 인스턴스에서 다시 게임을하도록 사용자에게 제공) 나는 Floating point exception (core dumped)이됩니다.

사용자가 선택 항목 1을 두 번 선택하면 발생합니다. x%0가 발생했을 때

#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

디버그 정보 (-g)로 코드를 컴파일 한 다음 gdb로 실행하고 충돌이 발생하면 백 트레이스를 살펴보십시오 (명령 : run, bt). – AntiClimacus

+0

만약'Dice :: roll()'이'slotNumber'를 0으로 설정했다면'roll'이 호출 될 때'rand() % slotNumber'가 0으로 나눕니다. 하지만 그래, 디버거를 사용하십시오. –

답변

1

부동 소수점 예외가 발생합니다

여기 내 코드입니다. 코드에서

는 여기에 동의하는해야합니다

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

케이스 : rand() 그것이

충돌 5
발은 0이되고 slot 번호는 0으로 설정됩니다, 다음 시간 roll()가 호출의 배수를 생성하는 경우
+0

'slotNumber = val ++'의 후행 증가는 완전히 무의미하다. 왜냐하면'val'이 증가 이후 범위를 벗어나기 때문이다. OP는'slotNumber = val + 1'을 원했을까요? – fredoverflow

관련 문제