LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2004, 07:13 AM   #1
DaveyB
Member
 
Registered: Aug 2004
Location: UK
Distribution: Debian
Posts: 83

Rep: Reputation: 15
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);


}
 
Old 11-04-2004, 07:22 AM   #2
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
You are going overbounds on your answer[] array.
 
Old 11-04-2004, 07:23 AM   #3
DaveyB
Member
 
Registered: Aug 2004
Location: UK
Distribution: Debian
Posts: 83

Original Poster
Rep: Reputation: 15
what you mean its allocated to 1000 characters and i am only using 4 (test), what should i do if you are right?
 
Old 11-04-2004, 07:27 AM   #4
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Hint: Your loop logic is incorrect.
 
Old 11-04-2004, 07:31 AM   #5
DaveyB
Member
 
Registered: Aug 2004
Location: UK
Distribution: Debian
Posts: 83

Original Poster
Rep: Reputation: 15
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.
 
Old 11-04-2004, 08:13 AM   #6
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 274

Rep: Reputation: 30
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); <
 
Old 11-04-2004, 08:40 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.

Last edited by Hko; 11-04-2004 at 08:42 AM.
 
Old 11-04-2004, 10:33 AM   #8
DaveyB
Member
 
Registered: Aug 2004
Location: UK
Distribution: Debian
Posts: 83

Original Poster
Rep: Reputation: 15
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;
}
 
Old 11-04-2004, 10:42 AM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.

Last edited by itsme86; 11-04-2004 at 10:44 AM.
 
Old 11-04-2004, 02:00 PM   #10
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation faulting? Brennan Linux From Scratch 8 10-05-2005 02:37 PM
Apache 2 Child Processes Seg faulting! DavidHayes Linux - Software 1 04-20-2005 07:54 PM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
Compiler seg faulting fitz9948 Slackware 2 05-11-2004 03:04 PM
My program is seg faulting? eggs Programming 2 10-06-2002 02:32 AM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration