Hello, I am on the verge of completing an assignment but I have run into a bit of a snag. I am to alter a program from my text so that if a letter is used twice, an error message will appear and call for another letter to be used. This is must store these letters in order to catch future errors.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//declare variables
string origWord = "";
string letter = "";
char dashReplaced = 'N';
char gameOver = 'N';
int numIncorrect = 0;
string displayWord = "-----";
string letterGuess[26];
//get original word
do //begin loop
{
cout << "Enter a 5-letter word in uppercase: ";
getline(cin, origWord);
} while (origWord.length() != 5);
//clear the screen
system("cls");
//start guessing
cout << "Guess this word: " <<
displayWord << endl;
while (gameOver == 'N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
//Entry Storage and Error Message. This is my problem.
for (int x = 0; x < 26; x++)
{
letterGuess[x] = letter;
if (letter == letterGuess[x])
{
cout << "Letter already entered. Choose another letter.";
break;
}
}
//search for the letter in the original word
for (int x = 0; x < 5; x += 1)
{
//if the current character matches
//the letter, replace the corresponding
//dash in the displayWord variable and then
//set the dashReplaced variable to 'Y'
if (origWord.substr(x, 1) == letter)
{
displayWord.replace(x, 1, letter);
dashReplaced = 'Y';
} //end if
} //end for
//if a dash was replaced, check whether the
//displayWord variable contains any dashes
if (dashReplaced == 'Y')
{
//if the displayWord variable does not
//contain any dashes, the game is over
if (displayWord.find("-", 0) == -1)
{
gameOver = 'Y';
cout << endl << "Yes, the word is "
<< origWord << endl;
cout << "Great guessing!" << endl;
}
else //otherwise, continue guessing
{
cout << endl << "Guess this word: "
<< displayWord << endl;
dashReplaced = 'N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
//add 1 to the number of incorrect guesses
numIncorrect += 1;
//if the number of incorrect guesses is 10,
//the game is over
if (numIncorrect == 10)
{
gameOver = 'Y';
cout << endl << "Sorry, the word is "
<< origWord << endl;
} //end if
} //end if
} //end while
system("pause");
return 0;
} //end of main function
The only way I can think of storing these letters is in an array. This seems to work, however, regardless of my input, I get my error message popping up. This happens when the letter is correct, when the letter is a duplicate, etc. It doesn't matter what is inputted, the error is displayed.
I've been tinkering with this for the past four hours. No matter how I change the loop from 36 to 39, that cout gets displayed regardless if the letter is wrong, right or repeated. I wish I knew what was wrong with this but my knowledge is extremely limited. This task has not been covered in the text, nor by my professor's lecture.
for (int x = 0; x < 26; x++)
{
letterGuess[x] = letter;
if (letter == letterGuess[x])
Umm...
So let me get this straight: You are setting a value in the array to the letter, and then testing if the same variable is equal to the same location in the array? Does something seem off there?
Its like:
Say I have x, and make it equal to y. If x==y, give an error...
Yeah, I see where you're coming from. Since I had no idea on how to go about this, I used examples I found on the net to come up with this. I don't know how to check the variable entered against the variables in the array.
I emailed my professor and he pretty much said the same thing but went on to talk about other aspects of the program...which have nothing to do with the task at hand. I need to check the letter entered against the array and then display a message if a letter has already been entered.
It's probably something really simple that I'm just overlooking.
for (int x = 0; x < 26; x++)
{ letterGuess[x] = letter;
for (int i = x; i < 26; i++)
{
if (i != x)
{
if (letterGuess[x] == letterGuess[i])
{
cout << "Letter already entered. Choose another letter.";
break;
}
}
}
}
Been tinkering with this for a while and came up with this loop. It looks...horrendous, but I'm not going for beauty here, just functionality...and it still falls short.
This loop does not display on every guess, correct or incorrect. However, it only records one entry into the array. For example...
User enters Z.
That's incorrect and the program asks again.
User enters C.
Incorrect, asks again.
User enters Z.
No error prompt.
So something is wrong with the way the letter entry is being stored into the array. On top of that, my break command seems to have...broken. It no longer stops the display from posting the full 26 error messages.
Well, at least some progress was made. Thanks NT3 for pointing that screw up out.
Worked on it a bit more and replaced the above with this...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Entry Storage and Error Message.
for (int x = 0; x < 26; x++)
{
if (letterGuess[x] == letter)
{
cout << "Letter already entered. Choose another letter."
<< endl;
}
if (letterGuess[x] != letter)
{
letterGuess[x] = letter;
x=27;
}
}
It no longer displays the error for each and every run but it only displays the error message when the same letter is entered twice, one after another. Something is wrong with the way data is being entered into the array.
All I need to fix is this last bit and I think it's done. Any pointers? I can't seem to figure out what needs to be inputted after the error message for the loop the proceed to the next element in the array.