I'm new at this. But I'm taking a class and I need to turn this in by Friday. Please help me. I can only use strings and arrays.
My hangman program needs five things:
1. user is prompted for a word that is ten letters or less, or else the program rejects it and asks for reentry.
2. if the word is too long or has a nonletter, the program rejects it and asks for reentry.
3. If the word has uppercase letters, it needs to be converted to lowercase
4. if there is more than 6 wrong guesses, the program ends.
5. If the user repeats a guess or enters a non letter, the program outputs a warning, but does not count it as a wrong guess.
So including those five things, the game is won when all the letters of the word is guessed.
Here's what I have so far:
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
bool guessed[26] = {false};
char guess;
char solution[11]; //the word that is inputted
int wrong_count(0);
cout << "Enter a word no more than ten letters: ";
cin >> solution;
while (true) //I tried writing && wrong_count <6, but it doesn't work.
{
cout << "Please enter your guess: " << endl;
cin >> guess;
cout << "You've entered the letter " << char(tolower(guess)) << endl; //converts uppercase into lowercase letters
if (bool (guessed [guess - 'a'] == true)) //I think I need a loop here, but I don't know where to start
cout << "You've guessed " << char(tolower(guess)) << " already." << endl;
else
guessed[guess - 'a'] = true;
for(int i = 0; i < strlen(solution); i++)
{
if (guess == solution[i])
cout << "correct" << endl;
else
wrong_count++;
}
}
if (wrong_count ==6)
cout << "You lose " << endl;
return 0;
}