| | |
hangman program stuck.. nid help.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
im new to C++ and i got an assignment which required me to do a hangman game. Global variables are not allowed. program must be modular, i.e create suitable functions. for functions, i was told to use reference parameters instead of pointer parameters. i am to use string class and not character array(e.g char x[20]).whenever possible, you are to use keyword 'const' for function parameters and prototype.
1. write a program (guess.cpp) that allows user to play the Hangman game, as follows:
a) program 1st prompts user to enter a number between 1 - 20, reads a word to guess from a file guess.txt, based on the number entered. the file has the following format:
word1 PrizeAmount
word2 PrizeAmount
word3 PrizeAmount
:
:
:
word20 PrizeAmount
b) program then prompts the user to guess a letter. if the letter is in the word, that letter will be displayed in the position(s) it occurs. if it is incorrect, the incorrect letter is added to the string of incorrect guesses and the number of attempts is incremented by 1.
c) if user guesses a letter which he/she already guessed before, program should display an error message and it should not increase the number of attempts.
d) the user wins if he/she guesses the word with no more than 7 incorrect guesses. if user wins, the prize amount is added to his earnings (initially set to $0). if user loses, the prize amount is deducted from his earnings and program wil display the word to guess.
e) program will quit if user's earnings is less than $0 or he chooses to quit the game after each round.
f) as an illustration, suppose the file guess.txt has the following content:
life 50
home 100
house 150
if user enters a number 3, the word to guess will be "house".
1. write a program (guess.cpp) that allows user to play the Hangman game, as follows:
a) program 1st prompts user to enter a number between 1 - 20, reads a word to guess from a file guess.txt, based on the number entered. the file has the following format:
word1 PrizeAmount
word2 PrizeAmount
word3 PrizeAmount
:
:
:
word20 PrizeAmount
b) program then prompts the user to guess a letter. if the letter is in the word, that letter will be displayed in the position(s) it occurs. if it is incorrect, the incorrect letter is added to the string of incorrect guesses and the number of attempts is incremented by 1.
c) if user guesses a letter which he/she already guessed before, program should display an error message and it should not increase the number of attempts.
d) the user wins if he/she guesses the word with no more than 7 incorrect guesses. if user wins, the prize amount is added to his earnings (initially set to $0). if user loses, the prize amount is deducted from his earnings and program wil display the word to guess.
e) program will quit if user's earnings is less than $0 or he chooses to quit the game after each round.
f) as an illustration, suppose the file guess.txt has the following content:
life 50
home 100
house 150
if user enters a number 3, the word to guess will be "house".
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
#include <iostream>
#include <fstream>
using namespace std;
const int max_tries=7;
int earnings=0;
int wordnum;
void getword ()
{
ifstream fin;
string word;
//int mark;
int value;
cin >> value;
fin.open("guess.txt");
if (!fin.good())
{
cerr << "File not found\n";
// return 1;
}
int i =0;
while(!fin.eof())
{
i++;
fin >> word;
if(i==value)
break;
}
cout << word << endl;
//cout << "Mark is : " << mark << endl << endl;
//return 0;
}
int main()
{
cout << "*** HANGMAN Guess a Word Game *** \n";
int NUM;
cout << "Enter a number between 1 to 20: ";
cin >>NUM;
if (NUM<1)
cout << "Too small. Please enter a number between 1 to 20: ", cin >> NUM;
if (NUM>20)
cout << "Too big. Please enter a number between 1 to 20: ", cin >> NUM;
cout << "Current earnings: $" << earnings << "\n";
cout << "Max number of tries is " << max_tries << "\n";
getword ();
cout << "*********************************" << endl;
system ("pause");
return 0;
}
#include <fstream>
using namespace std;
const int max_tries=7;
int earnings=0;
int wordnum;
void getword ()
{
ifstream fin;
string word;
//int mark;
int value;
cin >> value;
fin.open("guess.txt");
if (!fin.good())
{
cerr << "File not found\n";
// return 1;
}
int i =0;
while(!fin.eof())
{
i++;
fin >> word;
if(i==value)
break;
}
cout << word << endl;
//cout << "Mark is : " << mark << endl << endl;
//return 0;
}
int main()
{
cout << "*** HANGMAN Guess a Word Game *** \n";
int NUM;
cout << "Enter a number between 1 to 20: ";
cin >>NUM;
if (NUM<1)
cout << "Too small. Please enter a number between 1 to 20: ", cin >> NUM;
if (NUM>20)
cout << "Too big. Please enter a number between 1 to 20: ", cin >> NUM;
cout << "Current earnings: $" << earnings << "\n";
cout << "Max number of tries is " << max_tries << "\n";
getword ();
cout << "*********************************" << endl;
system ("pause");
return 0;
}
Last edited by jacksonwee; Oct 16th, 2007 at 11:36 am.
![]() |
Similar Threads
- Writing a Hangman game (C++)
- Help on a poker program (C++)
- help with hangman program (Java)
- Freezing Hangman Program?! + more... (Visual Basic 4 / 5 / 6)
- Please help with program - STUCK (C)
- Can u help me with hangman code in vb.net please :( (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Try to determine maximum and minimum values - and how many times they appear
- Next Thread: Adding things to Array-Based Lists
| 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 directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library 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 rpg simple string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






