Saturday, May 22, 2010

Help me with a c++ program plz?

Hey, I'm supposed to write a c++ program that plays the game hangman.


It's supposed to read in a bunch of words from a file that contains strings, it has to store the strings in an array of strings. Then print out a series of underscores and letters which represent the answer like s__ francis__. The user has to enter a letter, if it's incorrect print out a part of a man on the gallows with dashes and such. If it is correct put it in the word. Also I'm supposed to use 2 functions in the program. The file that the words will be read from will be up to 1000 lines long. And the program has to use strings to store the words not arrays.





Can someone plz explain how to do it or help me get started plz





Im not good with strings or arrays, im not sure how exactly i'm supposed to read stuff in from the file and store it in an array of strings

Help me with a c++ program plz?
Dude! This is not going to be a quick simple little program.





Id break it into chunks as follows:





1) Write a routine to read in the words and store them into the array of strings. Here are a couple of variables:


string myWords[1000];


short wordCount=0;


This could be a function or could just be at the beginning of main().





2) Write a function to display the gallows. This is probably the hardest part. I would suggest making your display just a few arrays of data and then assigning values as needed. For example, your gallow initialization could look something like


char gallows[10]={


" //========= ",


" || ",


" || ",


" || ",


" || ",


" || ",


" || ",


" || ",


"/||\ ",


"
Reply:
Reply:__________"}





Then, your routine would know that when one letter is wrong, it should change gallows[1], [2], and [3] to include characters for a head. When the second one is guessed wrong, 4, 5, and 6 get the body parts and so on.





The screen should be cleared each time you display the gallows. You should also display the letters under the gallows and guessed letters above it.





This means you'll need an array (size 26) for missed letters and another array, initialized with underscores and the size of the maximum word you expect.





You'll need to create another function to perform the letter check. This should make sure the letter hasn't already been used and if not, see if the letter exists in the word being guessed. If it doesn't, then the letter position corresponding to their guess is filled in within the missed letters array and the count (for the gallows display) is increased. If it is correct, replace the appropriate underscore with the correct letter.





There's an awful lot more to a program like this and it could take pages to provide more help. I'd suggest you work on small parts and then assemble those working parts to make the whole program.
Reply:You may want to make an object that contains a string and forward/backward pointers for a linked list. When you read a word, use new to get an instance of the object, put the word into the string member of the object and link the object to a list of other such objects. You can even sort the list if you need to. (It doesn't use arrays. Maybe that is what your instructor expects).





Be sure to delete all the objects in the list when you exit. It's good form.
Reply:The most eazy way is to use STL:





#include %26lt;string%26gt;


#include %26lt;vector%26gt;


using namespace std;





typedef vector %26lt;string%26gt; string_vector;


...


To add string to your array (i.e.vector):





void add_string( string_vector %26amp; sv, const string %26amp; s )


{


sv.push_back( s );


}





To get string from array:





string * get_string( string_vector %26amp; sv, int index )


{


size_t sz = sv.size();


if( !sz )


return 0;


if( index %26gt;= sz )


return 0;


return %26amp; sv[index];


}





...etc.





To learn more about STL, visit this site:


http://www.sgi.com/tech/stl/
Reply:Step 1: Learn how to read and write a file.


http://www.cplusplus.com/doc/tutorial/fi...





Step 2: Learn how strings work.


http://www.cppreference.com/cppstring/in...





Step 3: Learn how arrays work, indexing


http://www.cplusplus.com/doc/tutorial/ar...





This page might answer some questions too.


http://www.research.att.com/~bs/bs_faq2....


No comments:

Post a Comment