Hey! I just startet fildeling around with c++ a week or so ago. I have some experience in Java and python. Anyways, i wanted to make this simple hangman game;

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void exit(int exitcode);

char hus[] = {'h', 'u', 's'};
char kage[] = {'k', 'a', 'g', 'e'};

void test1(char Input, int done) //function to test answer
{ 
           r=0;            
            if (Input==kage[0]) {
                 if (Input==prog[0]) {
                 jump=1; 
                 goto kage0;
                 }
                 prog[0] = 'k';
                 r=1;
                 point = point+1;
                 kage0:;
                 if (jump==1) {
                 r=2;
                 cout<<"Uups, tried that one already did we?..\n \n";
                 }
                 }
            else if (Input==kage[1]) {
                 if (Input==prog[1]) {
                 jump=1; 
                 goto kage0;
                 }
                 prog[1] = 'a';
                 r=1;
                 point = point+1;
                 }
            else if (Input==kage[2])  {
                 if (Input==prog[2]) { 
                 jump=1;
                 goto kage0;
                 }
                 prog[2] = 'g';
                 r=1;
                 point = point+1;
                 }
            else if (Input==kage[3]) {
                 if (Input==prog[3]) {
                 jump=1; 
                 goto kage0;
                 }
                 prog[3]= 'e';
                 r=1;
                 point = point+1;
                 }
               else {  
                 cout<<" Wrong one, try another.\n";
                 r=0;
                 point = point-1;
                 }
}

void game1()
{
int done; //variable to test progression
string previous; //holds previous tried letters
previous =" ";
done=0;
liv=10;
char Input;
while (done!=4) {
cout<<prog[0]<<" "<<prog[1]<<" "<<prog[2]<<" "<<prog[3]<<"\n";
      if (liv==0) {  //tests if the player is dead
                    cout<<"You are dead (try again)"<<"\n";
                    point=point/increase;
                    goto game1;
                    }
cout<<"guess the word: \n";
cin>>Input;
previous= previous+Input;
cout<<"\n";
test1(Input, done); //calls 'test' function
            if (r==1) { //structure to count progression, saves in done
               done = done+1;
               }
            else if (r==0) {
                 liv=liv-1;
                 }
            if (done==4) { //tests if player has finished succesfully
                    cout<<"You made it :)\n";
                    point=point*increase;
                    cout<<prog[0]<<" "<<prog[1]<<" "<<prog[2]<<" "<<prog[3]<<"\n";
                    goto game1;
                    }
cout<<"Your changes left (0 means your dead): "<<liv<<"\n\n";
if (liv==5) {
                  cout<<"The guesses you have made so far: "<<previous;
                  cout<<"\n";
                  system("pause");
                  }
}
game1:;
}

int main()
{
int quit;
int n;
n=1;
quit=0;
srand(time(NULL));
rand();
      do {
               for(int q=0; q!=50; q++) {
                  prog[q] = '_';    //resets _ _ _   
                          }
              choose=rand()%3+0;
               if (choose==0){
                  game1();
                  n++;
                  }
               else if (choose==1) {
                    game2();
                    n++;
                    }
               else if (choose==2) {
                    game3();
                    n++;
                    }

Right now i need to add the two functions shown (void test, void game)along with each new word i wish to add to the game. What i need help with is i would like to save the words the user guesses in a file and let the program load it on every run. That would give me the option of making void test() more general, thus the code shorter and i wouldnt need to make two new functions per word i add to the game.
Thanks in advance!
/Krokcy EDIT: removed some of the code

Edited 6 Years Ago by Krokcy: n/a

You say you removed some of the code. You clearly did, but you removed too much. What's the prog[] array?

Way too many goto statements here. I'm not one of those purists who insist on no goto statements, but there needs to be a really good reason and all it does here is complicate the program flow IMO. When you have an array called kage[] and a label called kage0, it makes it that much harder to follow.

The inconsistent indentation also makes it very hard to follow the code.

I prefer this approach. You have two strings. One contains the real word, one contains the guessed word.

string word  = "Mississippi";
string guess = "???????????";

The user enters a guess: n

We check "Mississippi" for 'n'. None there, so increment the wrong guesses variable.

Now the user enters another guess: i

Go through the word variable and check for i. There are a bunch, so the guess is correct. Now update guess and display it:

guess = ?i??i??i??i

Now 'm'. Another good guess. Update guess:

guess = Mi??i??i??i


No gotos needed. Logic is easy to follow, plus less hard-coding of words in the function.

Good point ^^ But how do you test a string[] for a single letter?

You don't test a string[], you test a string. Just loop through every letter:

string word  = "Mississippi";
string guess = "???????????";

// miscellaneous code

char userLetterGuess = 'i';
int numInstaces = 0;
int numLetters = word.length();

for (int i = 0; i < numLetters; i++)
{
    if(word[i] == userLetterGuess)
    {
        guess[i] = userLetterGuess;
        numInstances++;
    }
}

Edited 6 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.