Using functions trouble

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2030 Views - Last Post: 28 April 2012 - 02:06 PM Rate Topic: -----

#1 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Using functions trouble

Posted 21 April 2012 - 03:58 AM

Hey all, i'm on this exercise that's giving me a bit of trouble.

I have to rewrite my Hangman game using functions. Include a function to get the player's guess and another function to determine whether the player's guess is in the secret word.

Hangman.cpp

// Hangman
// The classic game of Hangman

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

using namespace std;

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		cout << "\n\nEnter your guess: ";
		cin >> guess;
		guess = toupper(guess); //make uppercase since secret word in uppercase
		while(used.find(guess) != string::npos)
		{
			cout << "\nYou've already guessed " << guess << endl;
			cout << "Enter your guess: ";
			cin >> guess;
			guess = toupper(guess);
		}

		used += guess;

		if(THE_WORD.find(guess) != string::npos)
		{
			cout << "Thats's right! " << guess << " is in the word.\n";

			//update soFar to include newly guessed letter
			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;
		}
	}

	//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;
}





The thing is i'm not asking for you to do it for me. I'm asking if you guys can offer any assistance so i can start. I have no idea how to start, i'm lost at the moment.

Any tips, or hints would be much appreciated.

I haven't started yet so there is no code i can display :(.

Is This A Good Question/Topic? 0
  • +

Replies To: Using functions trouble

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Using functions trouble

Posted 21 April 2012 - 05:26 AM

Well basically they are asking you to write two functions that could look something like this.

string get_guess(string& letters_used) {
    //asks the player for a correct guess and return it
    //also update letters_used
}

bool check_guess(string word, string guess) {
    //Print relevant output, returns true if the guess is correct, otherwise false
}



The code that should go into these functions is basically all there. It's a restructuring excercise.

The second step would then be to write the main loop using these 2 functions.

This post has been edited by Karel-Lodewijk: 21 April 2012 - 05:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 26 April 2012 - 05:29 PM

Alright, well this is what i got. I somehow managed to get the function to get the player's guess. But, the real hard part is for the second function. I've tried by moving the code pieces like how the first function is done, but variables listed (THE_WORD and guess) won't be found in the new function. thinking of a way to get it done. any suggestions?

// Hangman
// The classic game of Hangman

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

using namespace std;
char getGuess();

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		guess = 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;

		if(THE_WORD.find(guess) != string::npos)
		{
			cout << "Thats's right! " << guess << " is in the word.\n";

			//update soFar to include newly guessed letter
			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;
		}
	}

	//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;
}


char getGuess()
{
	char response;
		cout << "\n\nEnter your guess: ";
		cin >> response;
		response = toupper(response); //make uppercase since secret word in uppercase

		return response;
}

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using functions trouble

Posted 26 April 2012 - 07:18 PM

You already have the code to test the guess in your original program, so you just have to move that part of the code out of main and into a separate function. The only complication is the need to pass some variables as parameters to the new function.

This functions tutorial should help you understand how to do that.
Was This Post Helpful? 1
  • +
  • -

#5 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 08:14 PM

ok after reading through the link u posted.

this is what i tried to do.
// Hangman
// The classic game of Hangman

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

using namespace std;
char getGuess();
char results(string a, char B)/>;

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		guess = 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;
		char z;
		z = results(THE_WORD, guess);
                z
			//update soFar to include newly guessed letter
			for(int i = 0; i < THE_WORD.length(); ++i)
			{
				if(THE_WORD[i] == guess)
				{
					soFar[i] = guess;
				}
			}
		
		
		{
			cout << "Sorry, " << guess << " isn't in the word.\n";
			++wrong;
		}
	}

	//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;
}


char getGuess()
{
	char response;
		cout << "\n\nEnter your guess: ";
		cin >> response;
		response = toupper(response); //make uppercase since secret word in uppercase

		return response;
}

char results(string a, char B)/>
{
	string c;
	char d;
	
		if(c.find(d) != string::npos)
		{
			cout << "Thats's right! " << d << " is in the word.\n";
		}

		return d;
}


i get some unusual errors wen i run this, and for some reason i believe i'm doing something wrong.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using functions trouble

Posted 27 April 2012 - 08:17 PM

When you are asking about errors, it would be helpful to post the exact error messages (if applicable), or if it's simply that you're not getting the results you wanted describe what you expected and what you got that was different.
Was This Post Helpful? 0
  • +
  • -

#7 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 08:25 PM

Oh ok, Well i'm getting Run-Time Check Failure #3 - The variable 'd' is being used without being initialized.

Didn't i initialize it here in the function?
char results(string a, char B)/>
{
	string c;
	char d;
	
		if(c.find(d) != string::npos)
		{
			cout << "Thats's right! " << d << " is in the word.\n";
		}

		return d;
}


i was aiming for it to at least display,
cout << "Thats's right! " << d << " is in the word.\n";
this. but it gives that error.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using functions trouble

Posted 27 April 2012 - 08:25 PM

In your results function

Quote

char results(string a, char b)
{
	string c;
	char d;
	
		if(c.find(d) != string::npos)
		{
			cout << "Thats's right! " << d << " is in the word.\n";
		}

		return d;
}


you're passing a string a and a char b to the function, so I'm guessing that you want to determine if a contains b, is that correct?

So I don't understand why you're declaring a new string c and a char d inside the function. Why aren't you using a and b in the if statement and in the cout statement? Wasn't that the reason for passing them to the function?

edit:
Regarding d, no you didn't initialize it, you declared it. Declaring just allocates a piece of memory to hold some type of data, but doesn't assign any specific value to it. Initializing entails assigning an initial value to a variable.

This post has been edited by r.stiltskin: 27 April 2012 - 08:33 PM

Was This Post Helpful? 1
  • +
  • -

#9 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 08:43 PM

Yes you are correct, i would like to determine whether a contains b.

I used c and d, because i thought i had to create new variables for a and b.

// Hangman
// The classic game of Hangman

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

using namespace std;
char getGuess();
string results(string a, char b, string c);

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		guess = 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;
		string z;
		z = results(THE_WORD, guess, soFar);
		results(THE_WORD, guess, soFar);    
	}

	//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;
}


char getGuess()
{
	char response;
		cout << "\n\nEnter your guess: ";
		cin >> response;
		response = toupper(response); //make uppercase since secret word in uppercase

		return response;
}

string results(string a, char b, string c)
{
	if(a.find(B)/> != string::npos)
		{
			cout << "Thats's right! " << b << " is in the word.\n";
			//update soFar to include newly guessed letter
			for(int i = 0; i < a.length(); ++i)
			{
				if(a[i] == B)/>
				{
					c[i] = b;
				}
			}
	   }

		return c;
}


1) when compiled i get these error message:

1>c:\users\twigz\documents\visual studio 2010\projects\chapter 5\chapter 5\hangman - rewritten.cpp(91): warning C4018: '<' : signed/unsigned mismatch
1>LINK : fatal error LNK1168: cannot open C:\Users\Twigz\documents\visual studio 2010\Projects\chapter 5\Debug\chapter 5.exe for writing
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2) My aim was the same, ,to let the function determine whether the player's guess is in the secret word.

i would love to see what the program displayed instead, but it won't run.
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using functions trouble

Posted 27 April 2012 - 08:51 PM

You can safely ignore the signed/unsigned warning.

There are no compiler errors in your code (other than a few b's that the forum replaced by B's).

The linker error LNK1168 has nothing to do with your code. You must have messed something up in your project.
Was This Post Helpful? 0
  • +
  • -

#11 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 09:06 PM

I think i'm making progress now with this code.

// Hangman
// The classic game of Hangman

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

using namespace std;
char getGuess();
string results(string a, char b, string c, int d);

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		guess = 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;
		string z;
		z = results(THE_WORD, guess, soFar, wrong);
		    
	}

	//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;
}


char getGuess()
{
	char response;
		cout << "\n\nEnter your guess: ";
		cin >> response;
		response = toupper(response); //make uppercase since secret word in uppercase

		return response;
}

string results(string a, char b, string c, int d)
{
	if(a.find(B)/> != string::npos)
	{
			cout << "Thats's right! " << b << " is in the word.\n";
			//update soFar to include newly guessed letter
			for(int i = 0; i < a.length(); ++i)
			{
				if(a[i] == B)/>
				{
					c[i] = b;
				}
			}
	}
	else
	{
		cout << "Sorry, " << b << " isn't in the word.\n";
		++d;
	}

	return c;
}


It displays whether the player's guess is in the secret word or not.

things i need to get done.
1) display the correct guesses so far.
2) change the number of incorrect guesses as the player guesses wrong.

Thanks for helping me this far.
Was This Post Helpful? 0
  • +
  • -

#12 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 09:38 PM

Yes!!!!! I got it!!!!!!!!

Here is my final draft.

// Hangman
// The classic game of Hangman

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

using namespace std;
char getGuess();
string results(string a, char b, string c);

int main()
{
	// setup
	const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

	vector<string> words; //collection of possible words to guess
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];           // word to guess
	int wrong = 0;                              // number of incorrect guesses
	string soFar(THE_WORD.size(), '-');         // word guessed so far
	string used = "";                           // letters already guessed

	cout << "Welcome to Hangman. Good luck!\n";

	// main loop
	while((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "\n\nYou have " << (MAX_WRONG - wrong);
		cout << " incorrect guesses left.\n";
		cout << "\nYou've used the following letters:\n" << used << endl;
		cout << "\nSo far, the word is:\n" << soFar << endl;

		char guess;
		guess = 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;
		string z;
		z = results(THE_WORD, guess, soFar); 
		soFar = results(THE_WORD, guess, soFar);

		if(THE_WORD.find(guess) == string::npos)
		{
			cout << "\nSorry, " << guess << " isn't in the word.\n";
			++wrong;
		}
	}

	//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;
}


char getGuess()
{
	char response;
		cout << "\n\nEnter your guess: ";
		cin >> response;
		response = toupper(response); //make uppercase since secret word in uppercase

		return response;
}

string results(string a, char b, string c)
{
	if(a.find(B)/> != string::npos)
	{
			cout << "\nThats's right! " << b << " is in the word.\n";
			//update soFar to include newly guessed letter
			for(int i = 0; i < a.length(); ++i)
			{
				if(a[i] == B)/>
				{
					c[i] = b;
				}
			}
	}

	return c;
}


There is only 1 little thing though.
it always displays this statement twice,
cout << "\nThats's right! " << b << " is in the word.\n";
.

Any ideas how i can adjust that?
Never mind i needed to simply remove,
string z;
		z = results(THE_WORD, guess, soFar);
. It wasn't being used for the return value, so i guess it maybe was just displaying the function as is. Correct me if i'm wrong.

This post has been edited by Twigz: 27 April 2012 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Using functions trouble

Posted 27 April 2012 - 09:51 PM

You can find that yourself.

1. Which part of your code generates that statement?
2. What triggers it? What makes that line of code run?
3. When you find the answer to 2, can you find some unnecessary code there?


OK, there you go. You found it.

This post has been edited by r.stiltskin: 27 April 2012 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#14 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 09:53 PM

View Postr.stiltskin, on 27 April 2012 - 09:51 PM, said:

You can find that yourself.

1. Which part of your code generates that statement?
2. What triggers it? What makes that line of code run?
3. When you find the answer to 2, can you find some unnecessary code there?


Yes after double checking i found that out. it was this line
string z;
z = results(THE_WORD, guess, soFar);



I believe this makes it run unnecessarily.


Thanks for all the help. I can finally move on to chapter 6 :)

This post has been edited by Twigz: 27 April 2012 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#15 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-July 11

Re: Using functions trouble

Posted 27 April 2012 - 10:04 PM

I have a second exercise regarding functions. Would u like to help me understand what it's asking me to do?

exercise 2:
Using default arguments, write a function that asks the user for a
number and returns that number. The function should accept a string
prompt from the calling code. If the caller doesn’t supply a string for the
prompt, the function should use a generic prompt. Next, using function
overloading, write a function that achieves the same results.

Mostly for this part - "The function should accept a string
prompt from the calling code. If the caller doesn’t supply a string for the
prompt, the function should use a generic prompt."
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2