LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Seg Faulting C. Where? (https://www.linuxquestions.org/questions/programming-9/seg-faulting-c-where-251007/)

DaveyB 11-04-2004 07:13 AM

Seg Faulting C. Where?
 
Some where in my code i am causing a memory violation and my program crashes. The word which is in the text file is test.

Can anyone help?


//This is going to be hangman
#include <stdio.h>
main()
{
char answer[1000];
char guess;
int bool;
int i;

FILE *file;
file = fopen("answer", "r");
if (file == NULL)
{
printf("No File To Be Processed \n");
}
else
{
printf ("File Found \n");
while(fgets(answer,1000,file)!=NULL)
{
printf("%s",answer);
}
}

printf ("Welcome To Hangman \n");
printf ("Please Pick A Letter \n");
guess = getchar();

do
{
i=i+1;
if(guess == answer[i])
{
printf("Correct Guess");
bool = 0;
}
else
{
printf("%c", answer[i]);
}
}
while(bool=1 || i < 3);


}

ugenn 11-04-2004 07:22 AM

You are going overbounds on your answer[] array.

DaveyB 11-04-2004 07:23 AM

what you mean its allocated to 1000 characters and i am only using 4 (test), what should i do if you are right?

ugenn 11-04-2004 07:27 AM

Hint: Your loop logic is incorrect.

DaveyB 11-04-2004 07:31 AM

Thanx for the hint, but as i am so new to C, i wouldn't know where or what i am looking for. Can you shed some more light on it, Thankyou.

Marius2 11-04-2004 08:13 AM

Re: Seg Faulting C. Where?
 
Quote:

Originally posted by DaveyB
Some where in my code i am causing a memory violation and my program crashes. The word which is in the text file is test.

Can anyone help?

[...]
}
while(bool=1 || i < 3);


}

Just a quick glance, so I'm not sure if this is the only mistake (sorry for sounding up-nosed :-))
but I think it should read

> while(bool==1 || i < 3); <

Hko 11-04-2004 08:40 AM

Code:

#include <stdio.h>

int main()  /* Doesn't matter too much, but strictly main() should
              * return an int.
              */

{
    char answer[1000];
    char guess;
    int bool;
    int i;

    FILE *file;
    file = fopen("answer", "r");
    if (file == NULL)
    {
        printf("No File To Be Processed \n");
    }
    else
    {
        printf ("File Found \n");
        while(fgets(answer,1000,file)!=NULL)
        {
            printf("%s",answer);
        }
    }

    /* Note: here answer[] will contain the last line of the file */

    printf ("Welcome To Hangman \n");
    printf ("Please Pick A Letter \n");
    guess = getchar();

    i = 0; /* Note: better to allways give a var a starting value */

    do
    {
        i=i+1;  /* Note: you're skipping first character here! */
        if(guess == answer[i])
        {
            printf("Correct Guess");
            bool = 0;
        }
        else
        {
            printf("%c", answer[i]);
        }
    }
    while(bool=1 || i < 3); /* You're assigning (=) 1 to bool, not comparing (==)
                            * the value of assignment will also be 1, so the
                            * loop continues indefinately, increasing i each time.
                            * When this is fixed, the loop will still continue
                            * indefinately if the 'guess' is not in 'answer[]'.
                            * In that case bool will not turn to 0, so will be
                            * 1 all the time, thus running the loop again.
                            *
                            *  It may be better to check for answer[i] != '\0'
                            */

    return 0;  /* return int from main() */


}

Compiling with -Wall option, would have given you a clue for at least one of the problems.

DaveyB 11-04-2004 10:33 AM

THANKYOU: I've done everthing you said and it still segfaults any ideas? :THANKYOU
 
//This is going to be hangman
#include <stdio.h>
int main()
{
char answer[1000];
char guess;
int bool;
int count;

//this is where i load all my variables to make my do loop work
bool = 1;

//reads in "test" from text file
FILE *file;
file = fopen("answer", "r");
if (file == NULL)
{
printf("No File To Be Processed \n");
}
else
{
printf ("File Found \n");
while(fgets(answer,1000,file)!=NULL)
{
printf("%s",answer);
}
}

//starts hangman
printf ("Welcome To Hangman \n");
printf ("Please Pick A Letter \n");
guess = getchar();

//validation to make sure the answer is correct to the inputted answer
do
{
count++;
if(guess == answer[count])
{
printf("Correct Guess");
bool = 0;
}
else
{
printf("%c", answer[count]);
}
}
while((bool == 1) || (count < 3));

return 0;
}

itsme86 11-04-2004 10:42 AM

Quote:

char answer[1000];
char guess;
int bool;
int count;

//this is where i load all my variables to make my do loop work
bool = 1;

//reads in "test" from text file
FILE *file;
This is bad. All variable declarations should go at the beginning of a code block. Declaring FILE *file after you do bool = 1 is a bad thing to do and will cause a lot of compilers to fail with an error.

Please use code tags when posting code.

Also, you don't initialize count. Automatic variables start with a garbage value so they must be initialized before you use them.

deveraux83 11-04-2004 02:00 PM

I just took a quick glance through your new code and you're still NOT giving all your variables a starting value. count is not being set to 0 before the increment operator (++) is being used. Hence, count could be any value and hence could very well be bigger than 1000 and hence crashing your program with a seg fault error.

Like what itsme86 is saying, good programming practise is to do all declaration of variables at the start quickly followed by their initialization to a starting value.


All times are GMT -5. The time now is 10:15 AM.