我不断得到下标超出范围不知道如何解决它[关闭]

Posted

技术标签:

【中文标题】我不断得到下标超出范围不知道如何解决它[关闭]【英文标题】:I keep getting subscript out of range don't know how to fix it [closed] 【发布时间】:2015-01-31 15:20:32 【问题描述】:
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

const int MAX_NUMS = 200;   // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";

char   inputLetter();
int    findChar(char letter, string&word);
string getGuessedWord(string&secretWord, string&lettersGuessed);
string getLettersGuessed(char letter, string&lettersGuessed, int n);
void display(string&lettersGuessed, string&wordGuessed, int num, int pos);
bool isDone(string wordGuessed);

int main()

string oneWord;                 // holds one word from input file
string secretWord;              // holds secret word to be guessed
string words[MAX_NUMS];         // holds list of words from input file
int randomValue;                // holds index of secret word
int count = 0;                  // holds number of words in the file

// Declare an ifstream object named myFile and open an input file
ifstream myFile;
myFile.open("P4Words.txt");

// Exit program if cannot open file for input
if (!myFile)

    cout << "Error: Unable to open file for input" << endl;
    return 0;


// Input words from a file into words array
// Add your code here ...
myFile >> oneWord;
while (!myFile.eof())

    words[count] = oneWord;
    count++;
    myFile >> oneWord;

myFile.close();
cout << count << " words loaded." << endl;

srand(static_cast<unsigned int>(time(0)));

// Select a secret word
// Add your code here ...
secretWord = words[rand() % (count + 1) ];

// Possible useful variables the loop
string lettersGuessed = ""; // holds letters guessed so far
string wordGuessed;         // holds current word guessed like �_ pp_ e�
int incorrectGuesses = 0;   // holds number of incorrect guesses so far
char letter;                // holds a guessed letter
bool done = false;          // have not guessed the word yet
int num = 8; int pos;
cout << "Welcome to the game, Hangman V1 by Your Name!" << endl;
cout << "I am thinking of a word that is " << secretWord.length()
    << " letters long." << endl;

// Set up a loop to input guesses and process
// Add your code here ...
do

    letter = inputLetter();
    pos = findChar(letter, secretWord);
    wordGuessed = letter;
    lettersGuessed = getLettersGuessed(letter, lettersGuessed, 8 - num);
    wordGuessed = getGuessedWord(secretWord, lettersGuessed);
    display(lettersGuessed, wordGuessed, num, pos);
    done = isDone(wordGuessed);
    num--;

 while ((num > 1) && (done == false));


// Check for won or lost
// Add your code here ...
if (done == false)

    cout << "sorry you lose..." << endl;

if (done == true)

    cout << "congratulations! " << endl;


system("pause");    // stop program from closing, Windows OS only

return 0;


// Add function definitions here ...
char inputLetter()

int i;
char letter;

do

    cout << "please guess a letter: " << endl;
    cin >> letter;

    for (i = 0; i < 25; i++)
    
        if (letter == LETTERS[i])
        
            return letter;
        
    

    if (letter != LETTERS[i])
    
        cout << "Oops! That is an invalid character." << endl;
    
 while (letter != LETTERS[i]);



int findChar(char letter, string &word)

int i = 0;  int pos = 0; bool found = false;
do

    if (word[pos] == letter)
    
        return pos;
        found = true;
    
    pos++;

 while (pos<word.length() - 1);

if (found == false)

    return -1;




string getGuessedWord(string&secretWord, string&letterGuessed)

string temp;
temp = secretWord;

for (size_t k = 0; k <= temp.length() - 1; k++)

    temp[k] = '_';

for (size_t i = 0; i <= temp.length() - 1; i++)

    for (size_t j = 0; j <= temp.length() - 1; j++)
    
        if (letterGuessed[i] == secretWord[j])
        
            temp[j] = letterGuessed[i];
        
    





return temp;

string getLettersGuessed(char letter, string&lettersGuessed, int n)


string temp;
temp = letter;
lettersGuessed.insert(n, temp);
return lettersGuessed;



void display(string&lettersGuessed, string&wordGuessed, int num, int pos)

if (pos != -1)

    cout << "You have " << num << " guesses left." << endl;
    cout << "Letters guessed so far: " << lettersGuessed << endl;
    cout << "Good guess!: " << wordGuessed << endl;
    cout << "-----------------------------------------------------" << endl;

if (pos == -1)

    cout << "You have " << num << " guesses left." << endl;
    cout << "Letters guessed so far: " << lettersGuessed << endl;
    cout << "Oops! that letter is not my word: " << wordGuessed << endl;
    cout << "-----------------------------------------------------" << endl;




bool isDone(string wordGuessed)

bool done = false; int k = 0;
for (size_t i = 0; i <= wordGuessed.length() - 1; i++)

    if (wordGuessed[i] == '_')
    
        k++;
    


if (k == 0)

    done = true;


return done;

它说下标超出范围我需要帮助来修复它

请告诉我如何解决它,这是一个即将到期的项目 这就是我到目前为止所得到的一切

【问题讨论】:

哪里出错了?您希望我们以可怕的格式找到它吗? + sscce.org 第一步将生成一个minimal示例。 【参考方案1】:

变化:

for (size_t i = 0; i <= temp.length() - 1; i++)

到:

for (size_t i = 0; i <= letterGuessed.length() - 1; i++)

【讨论】:

以上是关于我不断得到下标超出范围不知道如何解决它[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

我收到“字符串下标超出范围错误”。我不知道为啥

如何在我的 VBA 代码中解决这个“下标超出范围错误”?

我收到错误下标超出限制。我如何解决它?

字符串下标超出范围,使用散列 ADT

如何使用 mpi scatter 修复“矢量下标超出范围”?

如何在路径中获取最后一个文件夹时解决运行时错误9,下标超出范围