debug assertion failed

venwenn 2014-11-21 01:25:29
这是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!!
...全文
206 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻夢之葉 2014-11-21
  • 打赏
  • 举报
回复
引用 4 楼 jianwen0529 的回复:
[quote=引用 3 楼 venwenn 的回复:] [quote=引用 1 楼 jianwen0529 的回复:] 促发一个断点,原因是vector越界了! // create a game Hangman game(randw.get(testcaseNo-1)); 这句话你都没有对wordList进行存储东西就取了?
这个main code是老师给我们的,我们负责编写source code 和header file,有一个txt文件给我们,里面是一些单词可以random出一些word测试,有没有办法不动main code只修改source code或者header file 啊[/quote] 原本就没错啊! RandomWord randw; // load random words randw.RandomWord(); // 生成随机字串 // create a game Hangman game(randw.get(testcaseNo-1)); //这里至少要判断是否越界极其后续处理啊 [/quote] 不好意思,这个看错了。红色字体那个是构造函数我还以为是普通函数!
venwenn 2014-11-21
  • 打赏
  • 举报
回复
引用 4 楼 jianwen0529 的回复:
[quote=引用 3 楼 venwenn 的回复:] [quote=引用 1 楼 jianwen0529 的回复:] 促发一个断点,原因是vector越界了! // create a game Hangman game(randw.get(testcaseNo-1)); 这句话你都没有对wordList进行存储东西就取了?
这个main code是老师给我们的,我们负责编写source code 和header file,有一个txt文件给我们,里面是一些单词可以random出一些word测试,有没有办法不动main code只修改source code或者header file 啊[/quote] 原本就没错啊! RandomWord randw; // load random words randw.RandomWord(); // 生成随机字串 // create a game Hangman game(randw.get(testcaseNo-1)); //这里至少要判断是否越界极其后续处理啊 [/quote] 这个main函数老师会放在网页里,我们最后还要在网页里测试能不能通过,所以可能不会再加一个判断是否越界,有没有其他办法绕过这个
幻夢之葉 2014-11-21
  • 打赏
  • 举报
回复
引用 3 楼 venwenn 的回复:
[quote=引用 1 楼 jianwen0529 的回复:] 促发一个断点,原因是vector越界了! // create a game Hangman game(randw.get(testcaseNo-1)); 这句话你都没有对wordList进行存储东西就取了?
这个main code是老师给我们的,我们负责编写source code 和header file,有一个txt文件给我们,里面是一些单词可以random出一些word测试,有没有办法不动main code只修改source code或者header file 啊[/quote] 原本就没错啊! RandomWord randw; // load random words randw.RandomWord(); // 生成随机字串 // create a game Hangman game(randw.get(testcaseNo-1)); //这里至少要判断是否越界极其后续处理啊
venwenn 2014-11-21
  • 打赏
  • 举报
回复
引用 1 楼 jianwen0529 的回复:
促发一个断点,原因是vector越界了! // create a game Hangman game(randw.get(testcaseNo-1)); 这句话你都没有对wordList进行存储东西就取了?
这个main code是老师给我们的,我们负责编写source code 和header file,有一个txt文件给我们,里面是一些单词可以random出一些word测试,有没有办法不动main code只修改source code或者header file 啊
venwenn 2014-11-21
  • 打赏
  • 举报
回复
这是写一个hangman游戏的codeTT
幻夢之葉 2014-11-21
  • 打赏
  • 举报
回复
促发一个断点,原因是vector越界了! // create a game Hangman game(randw.get(testcaseNo-1)); 这句话你都没有对wordList进行存储东西就取了?
关于龙书第13章地形绘制的terrain项目运行出错问题 (注:龙书即:《DirectX9.0 3D游戏开发编程基础》) 在学习该教材时,当我们试着编译并运行13章地形绘制的terrain项目时,发现运行出错,并弹出了一个出错提示窗口,提示我们::访问vector 越界了 提示窗口的内容如下: -------------------------------------------------------------------- Microsoft Visual C++ Debug Library Debug Assertion failed! Program:...cuments and Settings\Administrator\Terrain\Debug\Terrain.exe File: d:\microsoft visual studio 10.0\vc\include\vector Line:932 Expression : vector subscript out of range For information on how your program can cause an assertion Failure , see the Visual C++ documentation on asserts. (Press Retry to debug the application) [终止(A)] [重试(R)] [忽略(I)] ---------------------------------------------------------------------------------- 问题主要出现在terrain.cpp 文件中的几个函数内部传递参数最终作为了vector的索引值,得到的索引值没有经过限定,导致超出了vector的界限-----最终访问越界. 修改的地方在下面这几处: 在terrain.cpp文档里搜索”//修改过!!!” bool Terrain::genTexture(D3DXVECTOR3* directionToLight) float Terrain::computeShade(int cellRow, int cellCol, D3DXVECTOR3* directionToLight) float Terrain::getHeight(float x, float z) 在terrain.cpp文档里搜索”//注意这里!!!” /int Terrain::getHeightmapEntry(int row, int col) bool Terrain::lightTerrain(D3DXVECTOR3* directionToLight) 在terrainDriver.cpp文档里搜索”//注意这里!!!” float height = TheTerrain->getHeight( pos.x, pos.z ); 我的修改主要是通过在传递索引值的地方,限定索引值的范围,这样就避免了vector访问越界,这也是龙书作者在写该13章的代码时一时所忽略掉的. 只修改过terrain.cpp文件中的内容,其他的都没动.所以可以只将terrain.cpp拷贝到工程中替换掉原来的就可以了.

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧