Re: Hangman Game - Feedback?
Code:
char actualword[50];
char guessedword[50];
string word;
You should use string for all of the string variables and get rid of the char arrays. Then you don't need things like this.
Code:
strcpy (actualword, word.c_str()); //Copy the STRING into the CHARACTER ARRAY for use below
Beginner or simple C++ programs that have things like strcpy() sprinkled in them IMO is a flawed program. This not only wouldn't be necessary, it is a hole in your program if word.c_str() is > 50 characters. Even if there are probably no words that have > 50 characters, you should get into the habit of using strings all the time, and only if there is a compelling reason to do so, you use arrays.
Code:
string actualword;
string guessedword;
string word;
//...
actualword = word; // no chance of buffer overrun.
Regards,
Paul McKenzie
Re: Hangman Game - Feedback?
Hi Paul,
Thanks very much for taking the time to help me out :]
The reason i chose to use character arrays for the actual and guessed word is so that i could run through them and check / change single characters by just using a for loop to loop through the elements in the array.
If i used strings for these 2 variables, what would be the best way of changing or checking individual letters in the string? and how could i refer to the character directly?
Thanks again
Re: Hangman Game - Feedback?
Quote:
Originally Posted by
Evo303
Hi Paul,
Thanks very much for taking the time to help me out :]
The reason i chose to use character arrays for the actual and guessed word is so that i could run through them and check / change single characters by just using a for loop to loop through the elements in the array.
You can do the same thing with string. If you couldn't, then std::string would be almost useless.
Code:
#include <string>
//...
int main()
{
std::string x = "abc123";
x[0] = 'b';
}
The string variable can be used just like an array of char.
Regards,
Paul McKenzie
Re: Hangman Game - Feedback?
Haha I like this. Not bad for someone just learning the basics.
Re: Hangman Game - Feedback?
Cheers Gonzo ;D
Paul, thank you very much for your help, i've changed those character arrays to strings now and everything still works nicely :)
Thanks again for your help. :thumb: