[CPP]// hangman showt in functions
// classic game of hangman
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
char playerGuess();
string checkWord();
const int MAX_WRONG = 8;// maximale fouten
vector<string> words;
int wrong = 0;
string used = " ";
char guess;
int main()
{
words.push_back("HANGMAN");
words.push_back("GUESS");
words.push_back("DIFFICULT");
srand(time(0));
random_shuffle(words.begin(),words.end());
const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-');
cout << "Welcome to hangman. Good Luck!\n";
//main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
playerGuess();
checkWord();
}
//afsluiten
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word whas " << THE_WORD << endl;
return 0;
}
char playerGuess() // vraagt player vor een gok.
{
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);// maakt hoofdletter omdat het woord erin is gespelt
while(used.find(guess) != string::npos)
{
cout << "\nYou've alredy guessed " << guess << endl;
cout << "enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
return guess;
}
string checkWord()
{
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << "is in the word.\n";
// update soFar om nieuwe letter toe te voegen.
for(int i = 0; i < THE_WORD.length(); ++i)
if(THE_WORD == guess)
soFar = guess;
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
[/CPP]
hoe kan ik
[CPP] const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-');[/CPP]
global zetten ? want als ik ze gewoon boven aan zet dan crasht het hele programmatje :S
zou ik ook uitleg kunnen krijgen waarom het programmatje crasht als ik hem opstart ?
// classic game of hangman
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
char playerGuess();
string checkWord();
const int MAX_WRONG = 8;// maximale fouten
vector<string> words;
int wrong = 0;
string used = " ";
char guess;
int main()
{
words.push_back("HANGMAN");
words.push_back("GUESS");
words.push_back("DIFFICULT");
srand(time(0));
random_shuffle(words.begin(),words.end());
const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-');
cout << "Welcome to hangman. Good Luck!\n";
//main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
playerGuess();
checkWord();
}
//afsluiten
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word whas " << THE_WORD << endl;
return 0;
}
char playerGuess() // vraagt player vor een gok.
{
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);// maakt hoofdletter omdat het woord erin is gespelt
while(used.find(guess) != string::npos)
{
cout << "\nYou've alredy guessed " << guess << endl;
cout << "enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
return guess;
}
string checkWord()
{
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << "is in the word.\n";
// update soFar om nieuwe letter toe te voegen.
for(int i = 0; i < THE_WORD.length(); ++i)
if(THE_WORD == guess)
soFar = guess;
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
[/CPP]
hoe kan ik
[CPP] const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-');[/CPP]
global zetten ? want als ik ze gewoon boven aan zet dan crasht het hele programmatje :S
zou ik ook uitleg kunnen krijgen waarom het programmatje crasht als ik hem opstart ?