Okay, I'm still new to C++ and I'm trying to make a simple hangman game for my class. Problem is I'm not sure where to go next in my code. Here is my code:
// Hangman.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <conio.h>
#include <time.h>
usingnamespace std;
//
//Hangman Game
string LoadFile() //To load the file
{
string W; //Declare Varibles
string words[20];
int count = 0;
ifstream txtfile; //the in file
txtfile.open("words.txt"); //opens file
for (count=0; !txtfile.eof(); count++)
{
getline(txtfile,W);
words[count] = W;
}
int k=(rand()%count); //Picks a random word from file.
txtfile.close(); //closes file
return words[k];
}
void RunGame(string word) //Runs the game
{
int k;
int count = 0;
char guess;
string letters;
char x;
for (k=0; k<word.length();k++)
{
letters+= '_';
}
cout << word << endl;
cout << endl;
while (count!=6)
{
for (x=0; x<15; x++)
{
cout << "Make a Guess" << endl;
cin >> guess;
for (k=0; k<word.length();k++)
{
if (guess == word[k])
{
cout << "Good Guess!" << endl;
letters[k] = guess;
}
else
{
cout << "Wrong Letter!" << endl;
count++;
}
}
cout << letters << endl;
}
}
cout << "Game Over! Your word was " << word << endl;
}
int _tmain(int argc, _TCHAR* argv[]) //main
{
cout << "Let's Play HangMan!" << endl;
srand(time(NULL));
string word= LoadFile();
RunGame(word);
cout << endl;
cout << "Thank You for Playing" << endl;
_getch();
return 0;
}
I'm using Microsoft Visual Studio 2008. I just don't know what I'm doing wrong. I can't seem to get it to stop after you guess the word correctly or how to make you lose after you have six wrong guesses. Please Help!!
A running loop in a game like this should test a simple boolean variable to see if the loop should continue or not. In this case you want to have "if(...)" checks inside of your running loop that would change the boolean to false if one of the conditions you specify are met.