Need some help with my program, supposed to be based on a game of hangman. Where the words that are used come from a file "hMan.in"
Mind you, this is a very, very early version of my project. And when compiled, it gives me no errors, however, when I run it, just after typing in the letter and hitting enter it gives me a segmentation fault and exits the program.
Tried commenting out parts of the program to narrow down my problem.
Trouble starts right after I un-comment out the part starting with:
if ( isThere == 0)
Any help would be greatly appreciated!!!
Here's the current version of the program:
Code:
#define STRSIZ 10
int
main(void)
{
/* Declaring Variables */
char inputStr[STRSIZ];
char dashStr[STRSIZ];
char guessStr[STRSIZ];
char guess, choice;
int isThere = 0, counter = 0, i, j, question;
int k = 0, full;
FILE* file_in;
file_in = fopen( "hMan.in", "r" );
/* Do while loop for exiting program */
do
{
/* Display the Greeting */
printf(" --------------------- \n");
printf("| Welcome to Hangman! |\n");
printf(" --------------------- \n");
/* Input word from file to string */
fgets(inputStr, STRSIZ + 1, file_in);
for (i = 0; i < strlen(inputStr); i++)
{
dashStr[i] = '_';
}
dashStr[i-1] = '\0';
/* Loop for game play */
do
{
/* Display word in dash form */
printf("Here is your word: ");
printf("%s\n", dashStr);
/* Prompt user for letter */
printf("Enter a letter: ");
scanf( " %c", &guess);
for ( i = 0; i < strlen(guessStr); i++)
{
fgets(inputStr, STRSIZ + 1, file_in);
for (i = 0; i < strlen(inputStr); i++)
{
dashStr[i] = '_';
}
dashStr[i-1] = '\0';
/* Loop for game play */
do
{
/* Display word in dash form */
printf("Here is your word: ");
printf("%s\n", dashStr);
/* Prompt user for letter */
printf("Enter a letter: ");
scanf( " %c", &guess);
for ( i = 0; i < strlen(guessStr); i++)
{
if ( guess == guessStr[i])
{
printf("Error: Already guessed!\n");
counter++;
isThere = 1;
break;
}
else
isThere = 0;
}
if ( isThere == 0)
{
guessStr[j] = guess;
for ( i = 0; i < strlen(inputStr); i++)
{
if ( guess == inputStr[i] )
{
dashStr[k] = guess;
counter++;
}
}
}
for ( i = 0; i < strlen(dashStr); i++)
{
if ( dashStr[i] == '_')
{
full = 0;
break;
}
else
full = 1;
}
counter++;
}
while( (counter < 10) && (full != 1) );
/* Asking user if they want to play hangman again */
do
{
printf( "Would you like to play again? (y/n):");
scanf( " %c", &choice);
switch ( choice )
{
case 'y':
case 'Y':
question = 1;
break;
case 'n':
case 'N':
question = 0;
break;
default:
question = 1;
printf("Error: Invalid option!!!\n");
break;
}
}
while( question != 0);
}
while( choice != 'n' && 'N');
return(0);
}