Hyena
May 14th, 1999, 10:30 AM
there is a minor problem with hangman; whenever a word is randomly chosen, if it is less then six letters, the program attaches junk to the end until it equals six characters (junk is random characters ' X . )what shall i do?
//program Hangman
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
//------------------definition of class hangman----------------------
class hangman {
public:
hangman();
//constructor
char* getWord(int&);
//gets word to be guessed
int getLength();
//gets length of word
void initTemp();
//creates temporary holder for unguessed characters
void checkGuess(char&, int&);
//checks user guess against inputted word
char* SecWord;
char* temp;
protected:
int letterOfSec;
};
//-------------------------------------------------------------------
//__________________-Hangman Member Functions-_____________________
/*****************************************************************/
hangman::hangman()
{
SecWord = temp = new char[13];
letterOfSec = 0;
}
/******************************************************************/
char* hangman::getWord(int& diff)
{
char *easy[5], *med[5], *hard[5];
for(int y = 0; y < 5; y++)
{
easy[y] = med[y] = hard[y] = new char[13];
}
easy[0] = "house";
easy[1] = "door";
easy[2] = "garage";
easy[3] = "bolo";
easy[4] = "john";
med[0] = "caucasian";
med[1] = "catastrophic";
med[2] = "dionysian";
med[3] = "accolade";
med[4] = "preprocessing";
hard[0] = "patrilinial";
hard[1] = "tendentious";
hard[2] = "portentious";
hard[3] = "euclydian";
hard[4] = "superannuated";
int randN;
randN = rand() % 5;
switch(diff)
{
case 1 :SecWord = easy[randN];
break;
case 2 :SecWord = med[randN];
break;
case 3 :SecWord = hard[randN];
break;
}
return SecWord;
}
/*******************************************************************/
int hangman::getLength()
//pre: SecWord has a value
//post: length of word is stored and returned
{
int x = strlen(SecWord);
return x;
}
/*******************************************************************/
void hangman::initTemp()
//pre: SecWord has a value
//post: a string of equal length, composed of '-' is created
{
letterOfSec = getLength();
for(int x = 0; x < letterOfSec; x++)
{
temp[x] = '-';
}
}
/******************************************************************/
void hangman::checkGuess(char& guess, int& check)
//pre: user enters a guess
//post: if guess is in SecWord, it is put into temp and check is
// incremented
{
if(islower(guess)) { guess = toupper(guess); }
for(int o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
if(isupper(guess)) { guess = tolower(guess); }
for(o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
}
/*******************************************************************/
//-------------------definition of class alpha----------------------
class alpha {
public:
initAlpha();
//constructs alphabet
void printAlpha();
//prints alphabet
void checkAlpha(char&);
//checks if guess is in alphabet, and replaces it with '*'
protected:
char letter[26];
};
//-----------------------------------------------------------------
//___________________-alpha Member Functions-_______________________
/******************************************************************/
alpha::initAlpha()
//post: alpha is initialized
{
char ch = 'A';
for(int y = 0; y < 26; y++)
{
letter[y] = ch;
ch++;
}
}
/******************************************************************/
void alpha::printAlpha()
//pre: array letter has values
//post: remaining alphabet is printed
{
for(int y = 0; y < 26; y++)
{
cout << letter[y] << " ";
}
}
/*******************************************************************/
void alpha::checkAlpha(char& guess)
//pre: array letter has been initialized
//post: guessed letter is replaced with an asterix
{
if(islower(guess)) { guess = toupper(guess); }
for(int t = 0; t < 26; t++)
{
if(guess == letter[t]) { letter[t] = '*'; }
}
}
/*******************************************************************/
//___________________________-MAIN-________________________________
void main()
{
int diff;
hangman h;
alpha a;
randomize();
cout << "------------------HANGMAN-------------------\n"
"Choose difficulty:\n"
"<1> Easy\n"
"<2> Medium\n"
"<3> Hard\n";
cin >> diff;
h.SecWord = h.getWord(diff);
h.initTemp();
a.initAlpha();
int check, turn;
char guess;
turn = check = 0;
do
{
cout << "\n" << h.temp << "\n";
a.printAlpha();
cout << "\nEnter your guess: ";
cin >> guess;
a.checkAlpha(guess);
h.checkGuess(guess, check);
turn++;
} while(check != h.getLength());
cout << "\n" << h.SecWord << "\n";
cout << "you win.\n"
"you took " << turn << " tries.\n";
}
//program Hangman
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
//------------------definition of class hangman----------------------
class hangman {
public:
hangman();
//constructor
char* getWord(int&);
//gets word to be guessed
int getLength();
//gets length of word
void initTemp();
//creates temporary holder for unguessed characters
void checkGuess(char&, int&);
//checks user guess against inputted word
char* SecWord;
char* temp;
protected:
int letterOfSec;
};
//-------------------------------------------------------------------
//__________________-Hangman Member Functions-_____________________
/*****************************************************************/
hangman::hangman()
{
SecWord = temp = new char[13];
letterOfSec = 0;
}
/******************************************************************/
char* hangman::getWord(int& diff)
{
char *easy[5], *med[5], *hard[5];
for(int y = 0; y < 5; y++)
{
easy[y] = med[y] = hard[y] = new char[13];
}
easy[0] = "house";
easy[1] = "door";
easy[2] = "garage";
easy[3] = "bolo";
easy[4] = "john";
med[0] = "caucasian";
med[1] = "catastrophic";
med[2] = "dionysian";
med[3] = "accolade";
med[4] = "preprocessing";
hard[0] = "patrilinial";
hard[1] = "tendentious";
hard[2] = "portentious";
hard[3] = "euclydian";
hard[4] = "superannuated";
int randN;
randN = rand() % 5;
switch(diff)
{
case 1 :SecWord = easy[randN];
break;
case 2 :SecWord = med[randN];
break;
case 3 :SecWord = hard[randN];
break;
}
return SecWord;
}
/*******************************************************************/
int hangman::getLength()
//pre: SecWord has a value
//post: length of word is stored and returned
{
int x = strlen(SecWord);
return x;
}
/*******************************************************************/
void hangman::initTemp()
//pre: SecWord has a value
//post: a string of equal length, composed of '-' is created
{
letterOfSec = getLength();
for(int x = 0; x < letterOfSec; x++)
{
temp[x] = '-';
}
}
/******************************************************************/
void hangman::checkGuess(char& guess, int& check)
//pre: user enters a guess
//post: if guess is in SecWord, it is put into temp and check is
// incremented
{
if(islower(guess)) { guess = toupper(guess); }
for(int o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
if(isupper(guess)) { guess = tolower(guess); }
for(o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
}
/*******************************************************************/
//-------------------definition of class alpha----------------------
class alpha {
public:
initAlpha();
//constructs alphabet
void printAlpha();
//prints alphabet
void checkAlpha(char&);
//checks if guess is in alphabet, and replaces it with '*'
protected:
char letter[26];
};
//-----------------------------------------------------------------
//___________________-alpha Member Functions-_______________________
/******************************************************************/
alpha::initAlpha()
//post: alpha is initialized
{
char ch = 'A';
for(int y = 0; y < 26; y++)
{
letter[y] = ch;
ch++;
}
}
/******************************************************************/
void alpha::printAlpha()
//pre: array letter has values
//post: remaining alphabet is printed
{
for(int y = 0; y < 26; y++)
{
cout << letter[y] << " ";
}
}
/*******************************************************************/
void alpha::checkAlpha(char& guess)
//pre: array letter has been initialized
//post: guessed letter is replaced with an asterix
{
if(islower(guess)) { guess = toupper(guess); }
for(int t = 0; t < 26; t++)
{
if(guess == letter[t]) { letter[t] = '*'; }
}
}
/*******************************************************************/
//___________________________-MAIN-________________________________
void main()
{
int diff;
hangman h;
alpha a;
randomize();
cout << "------------------HANGMAN-------------------\n"
"Choose difficulty:\n"
"<1> Easy\n"
"<2> Medium\n"
"<3> Hard\n";
cin >> diff;
h.SecWord = h.getWord(diff);
h.initTemp();
a.initAlpha();
int check, turn;
char guess;
turn = check = 0;
do
{
cout << "\n" << h.temp << "\n";
a.printAlpha();
cout << "\nEnter your guess: ";
cin >> guess;
a.checkAlpha(guess);
h.checkGuess(guess, check);
turn++;
} while(check != h.getLength());
cout << "\n" << h.SecWord << "\n";
cout << "you win.\n"
"you took " << turn << " tries.\n";
}