Thread: Segmentation Fault Error

  1. #1
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23

    Unhappy Segmentation Fault Error

    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);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >   if ( isThere == 0)
    >   {
    
    >      guessStr[j] = guess;
    The value of j is undefined, since you haven't initialized it.

  3. #3
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23
    thanks!
    Viva la Tutorial
    What is the meaning of life? 42 of course!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM