gpt4 book ai didi

C++ 异常 std::length_error 有时?

转载 作者:行者123 更新时间:2023-11-28 04:58:00 28 4
gpt4 key购买 nike

我正在尝试用 C++ 编写一个简单的 Hangman 游戏。游戏大部分工作,但有时当我调试它时,我得到异常 std::length_error。通常在用户输入选择后。由于 Stack Overflow 说我的大部分帖子都是代码,所以我在这里编写了相关代码。但如果需要,我会提供其余的。主要代码、变量和函数声明:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>

using namespace std;

void game(int choice);
void check(char letter, string word);
bool check_win(string word);

char guessed[30];
int guessCount = 0, tries = 0;

int main()
{
int choice, sentinel;

cout << "Welcome to Phobez's Hangman game!" << endl;
cout << "=================================" << endl;
cout << "Guess a word which is represented by a row of dashes, representing each letter of the word." << endl;
cout << "Pick a Topic" << endl;
cout << "1. Animals\n2. Colours\n3. Days of the Week\n4. Months\n5. Body Parts\n6. Numbers" << endl;
cout << "Choice: ";
cin >> choice;

game(choice);

system("pause");

return 0;
}

然后是负责设置和运行大部分游戏的 game() 函数:

void game(int choice) {
string word;
const int MAX_TRIES = 6;
char letter;

string animal[] = { "cat", "dog", "horse", "elephant", "duck", "turtle", "bird", "crab", "spider", "bear", "pig", "penguin", "mouse", "rabbit", "lion", "monkey", "bull"};
string colour[] = { "black", "white", "red", "yellow", "purple", "green", "blue" };
string day[] = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
string month[] = { "january", "february", "may", "march", "april", "june", "july", "august", "september", "october", "november", "december" };
string bodPart[] = { "head", "shoulder", "knee", "forehead", "eyebrow", "ear", "eye", "nose", "chest", "mouth", "arm", "stomach", "hand", "leg", "foot", "toe" };
string nums[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero" };

srand(time(0));

switch (choice) {
case 1:
word = animal[(rand() % 17) - 1];
break;
case 2:
word = colour[(rand() % 7) - 1];
break;
case 3:
word = day[(rand() % 7) - 1];
break;
case 4:
word = month[(rand() % 12) - 1];
break;
case 5:
word = bodPart[(rand() % 16) - 1];
break;
case 6:
word = nums[(rand() % 10) - 1];
break;
default:
word = animal[(rand() % 18) - 1];
}

do {
cout << "Word: ";

for (int i = 0; i < strlen(word.c_str()); i++) {
bool isPrinted = 0;

for (int j = 0; j < strlen(word.c_str()); j++) {
if (word[i] == guessed[j]) {
cout << word[i];
isPrinted = 1;
break;
}
}

if (isPrinted) {
continue;
}
else {
cout << "_";
}
}

cout << endl;
cout << "Misses: " << tries << endl;
cout << "Guess: ";
cin >> letter;

check(letter, word);

if (check_win(word)) {
cout << word << endl;
cout << "You won!" << endl;
break;
}
else {
continue;
}

} while (tries < MAX_TRIES);

if (tries >= MAX_TRIES) {
cout << "You lost!" << endl;
}
}

我尝试在 Google 和 Stack Overflow 上搜索有关此问题的帮助,但找不到可以向我解释我的代码有什么问题的。

错误只是偶尔弹出。大部分时间它都有效。我认为这是我的 switch case(随机整数生成器限制)中的一个错误,但我尝试将它们更改几次但没有区别。有人可以帮忙吗?这是我面临的唯一错误。提前致谢!

最佳答案

有了调试器,它运行得更快。但这里是演绎的方式:

  1. std::length_error当尝试超出某些对象的实现定义的长度限制时抛出,例如 stringvectormax_size。 (请注意,这不是超出范围的问题,而是存储对象所需大小的问题)。

  2. 您不使用 vector,您拥有的唯一字符串是 wordword 唯一可能超过存储限制的地方是您对其进行分配时。但是这个限制是如此之大,以至于你的数组中的小词不能触发它。

  3. 因此,这意味着您的数组访问不会访问您认为它访问的元素。所以一定是下标有问题。

  4. 我们在这里着陆 rand()它返回一个介于 0 和 RAND_MAX 之间的值。使用模数,您可以确保上限是可以的。但是,如果 rand() 返回 0 怎么办?然后您访问元素 -1,这是未定义的行为,也是意外崩溃的根本原因。

如何解决?

改变你的下标表达式。例如,在 animal[] 中,您有 17 个元素,索引从 0 到 16。因此,如果您将下标更改为 rand() % 17,您将得到一个介于0 和 16 保证在预期范围内。

你也可以使用 vector 而不是数组,这样可以避免手动计算其中的元素:

 <string> animal{ "cat", "dog", "horse", "elephant", "duck", "turtle", "bird", "crab", "spider", "bear", "pig", "penguin", "mouse", "rabbit", "lion", "monkey", "bull"};
...
word = animal[ rand()%animal.size() ]; // asuming that the vector is not empty :-)

P.S: strlen(word.c_str()) 可以替换为 word.size()

关于C++ 异常 std::length_error 有时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754234/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com