I am trying to take an array that is entered by a user and output blank lines that correspond to the number of characters that are in the entered word.
1 2 3 4 5 6 7
int game_play()//Initiates game play
{
char word[15];//Creates memory for the word that is to be guessed
cout<<"player 1, please enter a word"<<endl;
cin>>word;
cin.get();
}
This is all I have so far because I have no clue how to take this array and make it output blank characters. I have read the array tutorial, but it didn't really help. Any suggestions on what to do or where to look would be appreciated.
Since you are using an array, I would use cin.getline() which will null terminate the char array for you. Then you can use strlen() to get the size of the string.
If you want to output blank lines...
A c-style string (at least a normal one) is always null terminated so you might be able to cycle through the array, printing a newline for each execution of a loop until you find a null. But you'd probably be better off using the C++ string class and a length function (I believe there is one built in but I don't remember it). That way you are not limited to some arbitrary number of chars in the word.
EDIT: Yeah, it would be way more efficient to use strlen(). Personally though I prefer C++ string class which is more modern than old c-style "arrayofchar" strings.
Okay, what am I missing? Every time it says that there is 1 character and I need to figure out how to output blank lines ( _ ) for every character in the entered word.
1 2 3 4 5 6 7 8 9 10
int game_play()//Initiates game play
{
char word[15];//Creates memory for the word that is to be guessed
cout<<"player 1, please enter a word"<<endl;
cin>>word;
gets(word);
strlen(word);
cout<<"The word is "<<strlen(word)<<" characters long!"<<endl;
cin.get();
}
I recall reading that you should not use gets. Check the articles section and Duoas makes a reference to some articles by a fellow known as WaltP. You should use cin.getline(word), getline is efficient and reliable. Also it's kind of pointless to use both cin and gets like you have up there. Also why are you calling strlen(word) without putting anything on the left side? What is the point of that?
That's some style suggestions, but I have little experience with c-style strings so I cannot help you with the strlen. Check this website's reference section; therein you might find more data on how strlen works and what you are looking for.
Okay I fixed that. Thanks for the help, but is there a way to display a series of underscores that corresponds to the number of characters in the word. And is there a way to return the word to the entered by the user to the main program. This is for a homework assignment and my main program has to end up being less than 10 lines, not including comments.
Underscores are very simple. Just print in the exact same way you did the newlines but print underscores instead...
I don't remember how to return a c-style string but you can return a string class object pretty easy (I think). Just call the cstylestring constructor of the string class on your word and return the string. #include <string> at the beginning.
1 2 3 4 5 6 7 8 9 10
string game_play()//Initiates game play
{
char word[15];//Creates memory for the word that is to be guessed
cout<<"player 1, please enter a word"<<endl;
cin.getline(word);
cout<<"The word is "<<strlen(word)<<" characters long!"<<endl;
cin.get();
string ret(word);
return ret;
}
Correct me if I'm wrong, it's been a while since I programmed anything with strings...