been working on this code for 2-3 hours using the basic c++ information. tearing out my hair trying to figure out what is wrong writing hangman programm but trying to use functions to initialize programm and enter and response computer saying little or no error but wont launch can't figure out what to do plz help me. there are two types ive tried

OPTION 1:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

char getGuess();

string inWord();

int main()
{
    // set-up
    const int MAX_WRONG = 8; 

    vector<string> words;
    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");

	srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            
    int wrong = 0;                               
    string soFar(THE_WORD.size(), '-');          
    string used = "";                            
    
    cout << "welcome to hangman!!";
    

}char getGuess()
{
     char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess);
        return guess;
        }
        
string inWord()
        {
        vector<string> words;
        char guess;
        const string THE_WORD = words[0];  
        char guess1 = THE_WORD.find(guess);
        int wrong = 0;
        string soFar(THE_WORD.size(), '-');
        if (guess1 != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.\n";

            
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
        return soFar;
    }
  
   while ((wrong < MAX_WRONG) && (soFar != THE_WORD));
   {
         cout << "\n\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;
         
         getGuess()
         while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }
        
        used += guess;
        
        inWord()
        
            // shut down
    if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;
   
    return 0;
}

OPTION 2:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

char getGuess();

string inWord();

int main()
{
    const int MAX_WRONG = 8;  

    char guess;
    vector<string> words;  
    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");

	srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            
    int wrong = 0;                               
    string soFar(THE_WORD.size(), '-');          
    string used = "";                            
    
    cout << "welcome to hangman!!";
    
   while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
   {
         cout << "\n\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;
         
         getGuess();
         
         while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }
        
        used += guess;
        
        inWord();
        
            
    if (wrong == MAX_WRONG)
        
        cout << "\nYou've been hanged!";  
    else
        
        cout << "\nYou guessed it!";
    
    cout << "\nThe word was " << THE_WORD << endl;

    return 0;
}
}

char getGuess()
{
     char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess);
        return guess;
        }
        
string inWord()
        {
        vector<string> words;
        char guess;
        const string THE_WORD = words[0];  
        char guess1 = THE_WORD.find(guess);
        int wrong = 0;
        string soFar(THE_WORD.size(), '-');
        if (guess1 != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.\n";

            
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
		}
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
        return soFar;
    }

Recommended Answers

All 7 Replies

In OPTION 2 comment the following line
//inWord();
in your main function.

And learn how to use CODE-TAGS as you were told before.

Use code tags and we might help.

And you should really work on your code formatting. That looks terrible. ;)


And you should really work on your code formatting. That looks terrible. ;)

If the poster doesn't use code-tags, all formatting will be automatically lost, so the code might actually be properly formatted....

We dont use code-tags in our c++ tags but if you guys say they are suppose to be there i might have to self teach myself them because we are using he C++ beginning from thomson course technology. and it doesnt mention code-tags

If the poster doesn't use code-tags, all formatting will be automatically lost, so the code might actually be properly formatted....

and it doesnt mention code-tags

Code tags are a feature of Daniweb's forum. You take your code and wrap it in code tags like so when you want to post it here:

(code=cplusplus)
<code from your book here>
(/code)

This preserves the formatting of your code rather than trimming all leading whitespace from it. With the cplusplus attribute, line numbers and color coding are also added. Here's how code looks without code tags:

#include <iostream>

int main()
{
  for ( int i = 0; i < 10; i++ )
    std::cout<<"Hello, world!\n";
}

And here's the same code with code tags:

#include <iostream>

int main()
{
  for ( int i = 0; i < 10; i++ )
    std::cout<<"Hello, world!\n";
}

Notice the difference?

p.s. We give you about 10 posts to learn how to use code tags, then start issuing infractions for each failure to use them. At 2 points per infraction, that means you can get off five posts before you're automatically banned for not following our rules.

In the function inword() , i dont understand why you are creating new vector words and a character variable guess.

Instead of doing that. Pass them as arguments for the function or declare them globally and dont declare them again in the function itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.