I am having this huge issue with my Hangman Game program of a string being "out of range"...
What I can interpret about this is that something of the string has apparently an infinite number, furthermore, it can't store the letter you inputted to guess.
This is my Hangman code: (for further details)
#include <iostream>
#include <string>
using namespace std;
string Palabra;
int main() {
cout << "Bienvenido a 'Hangman'\n\nPor favor, entre su palabra para
adivinar: ";
getline(cin, Palabra);
Some nice introduction;
Here I am trying to get (user input of the "Word or phrase") to move on the next section.
int misses = 0;
int exposed = 0;
string display = Palabra;
for (int i = 0; i < display.length(); i++)
display[i] = '_';
"Misses" is the count of fails
"Loop For" Depending how long the Word will be, those will be replaced by Underline char. (Palabra is the same as Display)
After this, some are self-explanatory... but still I will explain.
When you guess correctly a letter, the underline (display) will be filled up with a letter of the user inputted Word (Palabra)
If you repeat, it will tell you that already that letter exists in the Word (Palabra)
If you fail to guess a letter, it will tell you "That letter doesn't exist" in the word
If you completely lost all six tries, it will show you the Word that was supposed to guess.
while (exposed < Palabra.length()) {
{ cout << "Miss: " << misses << ": ";
cout << "Adivina las " << display << " letras de la palabra o frase.\nTienes " << misses << " intentos.";
char response;
cin >> response;
bool goodGuess = false;
bool duplicate = false;
for (int i = 0; i, Palabra.length(); i++)
if (response == Palabra[i])
if (display[i] == Palabra[i])
{
cout << response << " esta en la palabra ya.\n";
duplicate = true;
break;
}
else {
display[i] = Palabra[i];
exposed++;
goodGuess = true;
}
if (duplicate)
continue;
if (!goodGuess) {
misses++;
{
cout << response << " no esta en la palabra.\n";
}
}
cout << "Si, la palabra fue " << Palabra << "." << endl;
return misses;
}
}
system("pause");
}
I can confirm that it works after the [getline (cin,Palabra)], but when I put a letter (for the guessing) , the error of "string subcript out of range" appears!
What's happening here?
Aucun commentaire:
Enregistrer un commentaire