functions?

Status
Niet open voor verdere reacties.

Biirra

Gebruiker
Lid geworden
25 dec 2009
Berichten
26
[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 ?
 
Waarom wil je er globale variabelen van maken? Je kan ze veel beter meegeven aan de functie checkWord.
[cpp]void checkWord(string, string &);
<snip>
checkWord(THE_WORD, soFar);
</snip>
void checkWord(string THE_WORD, string &soFar)[/cpp]

Het ampersandje (&-teken) is nodig zodat veranderingen aan de variabele "soFar" binnen de functie "checkWord" ook op de variabele "soFar" in de aanroepende functie van toepassing zijn.
Door van "checkWord" een void functie te maken los je ook de crash op (bij mij wel i.i.g.). Het probleem lijkt te zijn dat "checkWord" een string had moeten opleveren (het was een string functie), terwijl je dat niet deed.
 
heel erg bedankt, heeft egt geholpen en het werkt, bedankt voor de goeie uitleg. op deze manier leer je er ook nog eens van.

bedankt
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan