| | |
hangman game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 2
I was wondering if programming a hangman game would be possible. I took a little bit of time to make something, but it doesn't seem to be working very well. Any info is appriciated!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char solution[20]; //holds solution char blank[20]; //holds "*"'s for unsolved letters int counter = 0; //general-use counter int right = 0; //1 = right guess, 0 = wrong guess. char guess; cout<<"Enter phrase 20 chars or less."<<endl; cin.getline(solution, 20); int puzzLength = strlen(solution); //finds lengtrh of puzzle, stores INT value to puzzlength //convert puzzle to full uppercase for (counter = 0; counter < puzzLength; counter++){ solution[counter] = toupper(solution[counter]); } //done converting strcpy_s(blank, solution); //copy solution to the 'blank' array for (counter = 0; counter < puzzLength; counter++) { //converts characters to *'s to represent blanks if (isalnum(solution[counter])) blank[counter] = '*'; else blank[counter] = solution[counter]; } //closes for loop while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer cout<<endl<<"Solution phrase is: "<<solution<<"."<<endl; cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl; cout<<"Enter a guess."<<endl; cin>>guess; guess = toupper(guess); //cbeck guess! for (counter = 0; counter <= puzzLength; counter++) { if (guess == solution[counter]) { blank[counter] = guess; //fill in the puzzle with the letter } } //close loop, done checking guess } //game is over. cout<<"Winner!"; cin.get(); return 0; }
What isn't working very well? I tried the program and the only thing I found wrong is that it doesn't recognize it when you've solved the puzzle. That's because of this line:
You're comparing two pointers to the first address of the arrays with each other. In other words: this will never be true.
Change it to:
And it should work a lot better
A little advice: If you use C++ why not use std::string instead of char* ?
while (solution != blank) You're comparing two pointers to the first address of the arrays with each other. In other words: this will never be true.
Change it to:
while (strcmp(solution, blank)) And it should work a lot better
A little advice: If you use C++ why not use std::string instead of char* ?
Last edited by niek_e; Jun 6th, 2008 at 9:07 am.
•
•
Join Date: Mar 2008
Posts: 1,491
Reputation:
Solved Threads: 123
3 Threads posting the same problem.. ? Doesn't anybody try to do their homework by themselves anymore ?? You could have all helped eachother with it and you would figured out the problem in no time.
I need pageviews! most fun profile ever :)
![]() |
Similar Threads
- Writing a Hangman game (C++)
- Hangman project (VB.NET)
- hangman game in C++ (C++)
- newbie in need of help for hangman game (Python)
- Homework Help - Hangman Game (VB.NET)
- hangman revised (VB.NET)
- Need help with a certain part to a hangman game.. (Visual Basic 4 / 5 / 6)
- Help with while loop for hangman project (C++)
- Can u help me with hangman code in vb.net please :( (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Using ASIO Audio drivers
- Next Thread: Dynamic List class. A little help needed
Views: 1273 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






