Here is a c++ program I have been working on for an intro c++ class:

//hangman
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

//functions
//double index();
int main()
{
int const SIZE=22;
char name[20];
char dash[SIZE]={"                    "};
char q; //The User's Guess
int NumOfGuess=6; //Number of guesses
int flag;
int index1;

//Open text file called hangman.txt
ifstream myfile;
myfile.open ("hangman.txt");

myfile>>name;

//Open text file called index.txt
ofstream outputFile;
outputFile.open ("index.txt");
for (int index = 0; index<SIZE && name[index]!=' '; index++)
{
	if (toupper(name[index])>='A' && toupper(name[index])<='Z')
	{
	 dash[index]= '-' ;
	 outputFile<<index<<endl;
	}
}
//close file
outputFile.close();

for (NumOfGuess=6; NumOfGuess>0; NumOfGuess--)
{
cout<<"The Word is ";
for(int x=0;x<strlen(name);x++)
{
cout<<dash[x];
}

cout<<" "<<endl;

//Have the User make a guess
cout<<"Please make a guess: "<<endl;
cin>>q; //The User's Guess
cout<<" "<<endl;

flag=0;
//open file
ifstream myfileindex;
myfileindex.open ("index.txt");
for (int a=0; a<strlen(name); a++)
{
	if (q==name[a])
	 {
	 cout<<"Your guess was correct"<<endl;
	 cout<<" "<<endl;
	 //name[a]=dash[SIZE];
	 myfileindex>>index1;
	 name[a]=dash[index1];
	 NumOfGuess++;
	 flag=1;
	 }
myfileindex.close();
}
if (flag==0)
{
cout<<"Your guess was wrong."<<endl;
}
} //End of Guess counter



if(NumOfGuess>0)
{
cout<<"Game Over"<<endl;
cout<<"The word was "<<name<<endl;
}
else
{
cout<<"You Won"<<endl;
}


return 0;
}

Can some help me fix and improve the code. I am required to upload it to a service called hypergrade.

Here is the actual assignment:

Question 1

Hangman

For this assignment your are to write a program, using strings (from the string class) that plays
the popular game of Hangman. See the Wikipedia definition if
you have never played the game: http://en.wikipedia.org/wiki/Hangman_(game).

Here is how the program should work:

The word, phrase or sentence will be read in from a file. The filename will be read in from the console.
There will be only one word, phrase or sentence per file.The user guesses will be read in from the console.

(1) The program should display the line by hiding all letters (a-z, A-Z)
with a '-' (dash). All other characters should be displayed normally.
(2) The program should ask the player to make a guess.
(3) If the player makes a correct guess then the program should
redisplay the word, phrase or sentence with the letter displayed and ask
the player to make another guess
(4) If the palyer makes an incorrect guess then the program should tell
the player the guess was incorrect, subtract a guess and tell the user how
many guesses remain.
(5) If no more guesses remain then the program should print a message stating
that the game is over and displaying the original word, phrase or sentence.
(6) If more guesses remain then the program should ask the player to guess
again.
(7) The user will have a maximum of five guesses.

I have been using borland and dev-c++

Recommended Answers

All 4 Replies

Here is a report of my program after uploading it to hypergrade:

Can some help me fix and improve the code.

If you are getting any errors or Exception..post them

I am not getting any compiler errors but the test cases on hypergrade all fail.

Member Avatar for augustus7

(7) The user will have a maximum of five guesses. This is not the case in your code as the user gets 6 chances to guess.

I would suggest creating different test cases against the constraints given. Execute your program and tick off every constraint that it satisfies. That way you'll have a better idea of what it isn't doing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.