Oct 24, 2015 at 12:21am UTC
Hello everyone.
I need help getting a hangman program to work. The basis is that it is supposed to read from a file, select a word from the file at random, then the player must enter a letter to guess and they are either rewarded for getting it right or lose a life if they are wrong.
My problem, is this loop:
cout << "Guess a letter: " << endl;
for (int i = 0; i < 30; i++)
{
cin >> guess;
for (int j = 0; j < sentence.length(); j++)
{
if (guess == sentence[j])
{
blank[j] = guess;
cout << blank << endl;
if (blank == sentence)
{
char again;
win = win + 1;
cout << "You win!" << endl;
cout << "Do you want to play again? <y/n>" << endl;
cin >> again;
cin.clear();
cin.ignore();
system("CLS");
if (again == 'y')
{
return main();
}
else if (again == 'n')
{
return 0;
}
}
}
else if (guess != sentence[i])
{
attempts = attempts--;
cout << attempts;
if (attempts == 6)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 5)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 4)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /| " << endl;
cout << " | / | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 3)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 2)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / " << endl;
cout << " | / " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 1)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;;
}
if (attempts == 0)
{
cout << " +------------+ " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
cout << "You've been hung. Game over." << endl;
loss = loss + 1;
cin.get();
system("CLS");
return main();
}
The problem is, when the program checks for a letter, it checks every part of the word. That's fine, but if it marks any letters that aren't the one inputted by the user as misses.
When doing hits and misses by themselves, the program works fine. But when together, they cause a problem.
Example:
The selected word is 'apple',
User guess 'p'
Result = -pp-- and 3 strikes.
If anyone can help me with this, I would really appreciate it.
Oct 24, 2015 at 12:23am UTC
The programs code itself is here: #include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
int choice;
int word;
int attempts = 7;
int win = 0;
int loss = 0;
int main()
{
cout << "Welcome to Hangman!" << endl;
cout << endl;
cout << "Please enter one of the following numbers: " << endl;
cout << endl;
cout << "1 = New Game" << endl;
cout << "2 = Player Scores" << endl;
cout << "3 = Quit Game" << endl;
cout << endl;
cin >> choice;
system("CLS");
if (choice == 1)
{
srand(time(NULL));
ifstream file;
string line[30];
string blank;
string sentence;
string name;
char guess;
cout << "Please enter your name: " << endl;
cin >> name;
system("CLS");
cout << "Hello " + name + "! Please choose one of the following topics: " << endl;
cout << "1 = First Names" << endl;
cout << "2 = 80's Rock Songs" << endl;
cout << "3 = Animals" << endl;
cout << "4 = Random" << endl;
cout << "5 = Main Menu" << endl;
cin >> choice;
system("CLS");
if (choice == 1)
{
file.open("First.txt");
if (file.is_open())
{
for (int i = 0; i < 30; i++)
{
file >> line[i];
}
word = rand() % 29;
blank = line[word];
sentence = line[word];
for (int i = 0; i < blank.length(); i++)
{
blank[i] = '-';
}
cout << blank << endl;
cout << sentence << endl; // Delete later
cout << "Guess a letter: " << endl;
for (int i = 0; i < 30; i++)
{
cin >> guess;
for (int j = 0; j < sentence.length(); j++)
{
if (guess == sentence[j])
{
blank[j] = guess;
cout << blank << endl;
if (blank == sentence)
{
char again;
win = win + 1;
cout << "You win!" << endl;
cout << "Do you want to play again? <y/n>" << endl;
cin >> again;
cin.clear();
cin.ignore();
system("CLS");
if (again == 'y')
{
return main();
}
else if (again == 'n')
{
return 0;
}
}
}
else if (guess != sentence[i])
{
attempts = attempts--;
cout << attempts;
if (attempts == 6)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 5)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 4)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /| " << endl;
cout << " | / | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 3)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 2)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / " << endl;
cout << " | / " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
}
if (attempts == 1)
{
cout << " +------------+ " << endl;
cout << " | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;;
}
if (attempts == 0)
{
cout << " +------------+ " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | /|\\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " =============== " << endl;
cout << "You've been hung. Game over." << endl;
loss = loss + 1;
cin.get();
system("CLS");
return main();
}
}
}
}
}
}
}
}