954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

function not working cant figure problem

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;
    }
chickenlord500
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

RenjithVR
Light Poster
41 posts since Mar 2008
Reputation Points: 12
Solved Threads: 7
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Use code tags and we might help.

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

hacker9801
Junior Poster
130 posts since Aug 2007
Reputation Points: 59
Solved Threads: 16
 
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 codemight actually be properly formatted....

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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....
chickenlord500
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

>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]

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

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You