hey i suck at this, i need to write a program that plays the game hangman, i wrote some of it, not even sure if any of it is right though.
I'll post the programs and then I'll ask questions.
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main(){
string myWords[1000];
short wordCount=0;
ifstream word;
word.open("dictionary.txt");
while (!word.eof()){
word%26gt;%26gt;myWords;
wordCount++;
}
int x = rand() % wordCount + 0;
string question = myWords[x];
return 0;
}
void drawGallows(char a[]){
char gallows[10]{
" //========= ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
" || ",
"/||\ ",
"
Can someone help me with a c++ program please?
Reply:__________";
}
Reply:Well, you seem to have your gallows on the right track, so I'll omit that. My example uses streamstreams in lieu of a file since I didn't feel like creating a file (and you already have the code for that).
For the answer, I'm taking the string's length and turning all its characters into asterisks. Once a letter is found, the string 'ourword' character is also turned into an asterisk. Once all the characters in 'ourword' are asterisks, then it matches the answer, ie. * * * * * *.
[code]
#include %26lt;string%26gt;
#include %26lt;sstream%26gt;
#include %26lt;iostream%26gt;
int found;
string ourword; //for example, copies an array word for puzzle
string answer; //used for comparison
int wrong=0; //must initialize this for increment
void startgame()
{
std::cout%26lt;%26lt;"
Reply:||\n"; //build this function to display hangman
}
//////////////////////////////////////...
string%26amp; FindChar(string%26amp; look_for_letter,
const string%26amp; findMe) {
//start at pos 0
size_t letter = look_for_letter.find(findMe, 0);
// look for letter
if (letter != string::npos) //string, find and replace function
found=1;
else
found=0;
}
//////////////////////////////////////...
string letter; //we declare this globally since it's used both
//inside and outside of call_for_letter function.
void call_for_letter()
{
letter.erase();
std::cout%26lt;%26lt;"please enter a letter";
std::cin%26gt;%26gt;letter;
}
std::string sentence="I am a string and I want to add myself into"
" an array ";
std::stringstream as; //in lieu of reading from file I'm using a
//stringstream - I'm declaring that here.
std::stringstream ar; //we'll use this stream to buffer sentence
//for reading into an array
std::string word; //our string word once found
std::string fullmonty[1024]; //size limit
std::string billy [13]; //our array size -- arrays begin at 0, not 1!
std::string entry; //our string to store underscore display
std::string underline="_"; //character to use for underscore
std::string asterisk="*"; //character to use for asterisk
int main ()
{
as %26lt;%26lt; sentence; //insert our sentence of words into stringstream
ar %26lt;%26lt; sentence;
for (int n=0 ; n%26lt;13 ; n++ ) //n12 - do not exceed array size
{
std::getline(as,word,' '); //word stops at whitespace
billy[n]=word; //begin inserting words into array beginning at 0
//as for loop advances, so does our array #
std::cout %26lt;%26lt; billy[n]%26lt;%26lt;std::endl; //print each array at its stored
}
std::cout%26lt;%26lt;std::endl;//for display spacing
std::cout%26lt;%26lt;billy[3]; //for show that array is stored.
std::cin.get(); //pause to look
ourword=billy[3]; //copy this array to ourword for puzzle word
//make underscores for display (entry string)
for (int y=0; y%26lt;ourword.length(); y++){ //use array.string
//length to determine how many underscores should be inserted
// into empty 'entry' string. Same for asterisks
entry.insert(0,underline,0); //insert underscores
answer.insert(0,asterisk,0); //insert asterisk *use to compare
//a completed answer
}
std::cout%26lt;%26lt;std::endl;
std::cout%26lt;%26lt;entry; //displays underscore (word length)
std::cin.get();
//add entire sentence into an array
std::getline(ar,word);
fullmonty[1024]=word;
std::cout%26lt;%26lt;std::endl;
std::cout%26lt;%26lt;fullmonty[1024];
std::cin.get();
//let's start a game
while (letter!="quit"){ //so long as user doesn't type in 'quit'
startgame();
call_for_letter(); //call our input function
FindChar(ourword,letter); //look for letter
string::size_type loc= ourword.find(letter); //id location
if( loc!= string::npos )
{
ourword.replace(loc,1,"*");
entry.replace(loc,1,letter);
std::cout%26lt;%26lt;std::endl;
std::cout%26lt;%26lt;ourword; //we replaced our found letter with an asterisk
std::cout%26lt;%26lt;std::endl;
std::cout%26lt;%26lt;entry; //above, we've replaced an underscore with letter found
startgame(); //return to input another letter
}
else{
std::cout%26lt;%26lt;"letter not found";
//here, you would handle wrong answer, count them,
//change hangman configuration in startgame strings.
wrong++; //keep track of wrong letters
std::cin.get();
startgame(); //return to input another letter if wrong answers are below min.
}
if (ourword==answer)
{
std::cout%26lt;%26lt;"You win!";
cin.ignore();
cin.get();
return 0;
}
}
return 0;
}
[/code]
Reply:I think you can consult a freelancers to get your code working
from
http://expert.myitcareer.org/
flower girl dresses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment