debug assertion failed
这是header file
#include<string>
using namespace std;
class Hangman
{
// FILL IN
public:
Hangman ( string word );
void setdisplay_word ();
void display () ;
bool guessLetter( char key );
bool done () const;
int getWordLength () const;
int getNumGuesses () const;
int getNumRevealedLetters () const;
int getNumCorrectGuesses () const;
bool getWinLose () const;
private:
string word, guesses, misses_letter, display_word;
int NumGuesses, NumCorrectGuesses, NumRevealedLetters, misses;
};
这是source code
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept> // for illegal_argument exception class
#include "Hangman.h"
using namespace std;
// Hangman constructor
Hangman::Hangman (string word ) {
int NumGuesses = 0, NumCorrectGuesses = 0, NumRevealedLetters = 0, misses = 0;
this -> word = word;
string misses_letter;
string display_word;
display_word = word;
for (int i = 0; i < getWordLength(); i ++ ){
display_word [i] = '-';
}
}
void Hangman::display () {
cout << "Round " << NumGuesses << '\n';
cout << "Word: " << display_word << '\n';
cout << "Misses: " << misses_letter << '\n';
try {
if (getWinLose ())
cout << "You WIN! Congratulations!\n";
else if (getNumGuesses() - getNumCorrectGuesses () >= 6)
cout << "You LOSE! The word was " << word << "." << '\n';
}
catch (runtime_error &e) { cout << e.what() << '\n'; }
}
bool Hangman::guessLetter(char key ) {
bool guessed_input = false;
for ( int n = 0; n < getWordLength (); n ++ ){
if ((key == display_word [n]) || (key == display_word [n] + 32 ) || (key == display_word [n] - 32 )){
guessed_input = true; // repeated guess of same character
}
}
if (done () == true){
throw runtime_error ( "game is finished");
}
else if ((key < 'A') || ((key > 'Z') && (key < 'a')) || (key > 'z')){
cout << "Not a letter! Try again." << '\n';
}
else if (guessed_input = true){
cout << "You already guessed '" << key << "'! Try again."<< '\n';
}
else {
bool goodGuess = false;
for (int k = 0; k < getWordLength (); k ++ ){
if ((key == word [k]) || (key == word [k] + 32 ) || (key == word [k] - 32 )){
display_word [k] = word [k];
goodGuess = true; // guess is correct
NumRevealedLetters ++;
}
}
NumGuesses ++;
if (!goodGuess){
misses_letter.push_back( key );
bool insert_input = true;
for (size_t m = 0; insert_input && (m <= misses_letter.length() - 1); m ++ ){
if(key < misses_letter.at( m )) {
misses_letter.insert(m, 1, key);
misses_letter.pop_back();
insert_input = false;
}
}
misses ++;//keep track of misses
}// guess is incorrect
}
cout << '\n';
return false;
}
bool Hangman::done () const{
if ((getNumGuesses () - getNumCorrectGuesses () >= 6) || (getWordLength () == getNumRevealedLetters ()))
return true;
else
return false;
}
int Hangman::getWordLength () const {
return word.length();
}
int Hangman::getNumGuesses () const {
return NumGuesses;
}
int Hangman::getNumRevealedLetters () const {
return NumRevealedLetters;
}
int Hangman::getNumCorrectGuesses () const {
return NumCorrectGuesses;
}
bool Hangman::getWinLose () const {
if (getNumRevealedLetters () == getWordLength ())
return true;
else if (getNumGuesses() - getNumCorrectGuesses () >= 6)
return false;
else throw runtime_error("game not finished");
}
这是main
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include "Hangman.h"
using namespace std;
const bool RANDOM = true;
/***********************************************/
/* Class for generating random words */
/***********************************************/
class RandomWord {
private:
vector<string> wordList;
public:
RandomWord() {
// load word file
string line;
ifstream myfile ("wordsEn-PASS.txt");
if (myfile.is_open()) {
while ( getline (myfile,line) ) {
line.erase(line.find_last_not_of(" \n\r\t")+1); // remove white space
wordList.push_back(line);
}
myfile.close();
} else {
throw invalid_argument("missing file");
}
// set the random seed
if (RANDOM)
srand(time(NULL));
else
srand(0);
}
// get a random word
string get() {
int i = rand() % wordList.size();
return wordList[i];
}
string get(int i) {
return wordList[i];
}
};
/*** MAIN FUNCTION *********************************************/
int main() {
int testcaseNo=0;
cin>>testcaseNo;
RandomWord randw; // load random words
// create a game
Hangman game(randw.get(testcaseNo-1));
// run game loop
while (1) {
game.display(); // display the current game state
if ( game.done() ) { // exit loop if game finished
break;
} else {
char key;
do {
cout << "Guess: ";
cin >> key; // get input key
cin.ignore(INT_MAX, '\n');
} while (game.guessLetter(key)); // send key to game
// repeat if invalid input
}
}
}
急求大神,被搞惨了,进入程序输入word按完回车老是出现debug assertion failed,说 vector out of range!!