ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I am running the following program with the Test Delete function, and it works, except for when it gets to the end of the list, then it seg faults if the answer input is wrong (if the CheckAnswer if statement is evaluated): Does anyone know why this seg fault occurs?
Code:
int BeginQuizFirstTry (NODEPTR *headListPtr1,
NODEPTR *headListPtr2,
int numberOfTotalQuestions)
{
/* Display a message indicating that the quiz has
begun for the user. */
printf ("Here are your questions. Good luck !!!\n\n");
NODEPTR curr = *headListPtr1;
NODEPTR temp;
char questionAnswer [NUMBEROFANSWERCHARACTERS];
char quizCorrectAnswer [NUMBEROFANSWERCHARACTERS];
int questionCounter = 0;
int correctAnswerCounter = 0;
QUIZ deletedQuestion;
while (curr != NULL)
{
questionCounter++;
printf (" %d. ", questionCounter);
DisplayQuestion (curr);
scanf ("%s", questionAnswer);
strcat (questionAnswer, "\n");
if (CheckAnswer (curr, questionAnswer,
&correctAnswerCounter) != 1)
{
strcpy (deletedQuestion.question, curr -> data.question);
strcpy (deletedQuestion.answer, curr -> data.answer);
temp = CreateNode ();
SetData (temp, deletedQuestion);
Insert (headListPtr2, temp);
strcpy (quizCorrectAnswer, curr -> data.question);
if (curr != NULL)
{
curr = curr -> next;
}
TestDelete (headListPtr1, quizCorrectAnswer);
}
else
{
/* Set the node pointer referencing the current node
(the current question in the quiz) to point to the
next node in the list, or the next question in the
quiz. */
curr = curr -> next;
}
}
printf ("\n\n%d", correctAnswerCounter);
return 1;
}
int TestDelete (NODEPTR *headPtr, char *target)
{
NODEPTR temp, prev, curr;
if (IsEmpty (*headPtr))
{
printf ("Empty List\n\n");
return (-1);
}
else if (strcmp ((*headPtr) -> data.question, target) == 0)
{
temp = *headPtr;
*headPtr = (*headPtr) -> next;
free (temp);
return 1;
}
else
{
prev = *headPtr;
curr = (*headPtr) -> next;
while (curr != NULL && strcmp (curr -> data.question, target))
{
prev = curr;
curr = curr -> next;
}
if (curr != NULL)
{
temp = curr;
prev -> next = curr -> next;
free (temp);
return 1;
}
else
{
printf ("%s not found\n\n", target);
return (-1);
}
}
}
Mr Mistro... i'm getting a little concerned by the number and type of questions you're asking on LQ.org. It looks an awful lot like you're doing a school course in C++, and if that's the case we are NOT here to do your homework for you. You will have been provided notes as part of a school course to cover everythign you are deemed to need to know.
Yes, this is a project for school; however, I understand that you are not here to "do" my school work for me. But I thought this forum was on subject specific questions about programming, which is exactly what we have here, not write my program for me.
If I understand you correctly, only things interdependent from course work can be asked here? Well thats just rediculous because no where on here am I asking anyone to write anything. I am asking for suggestions on a problem, I'm not asking for any "hand me outs".
And the number and type of questions? I post here every day/week almost, answering people's questions and posting some of my own which are sometimes and sometimes not related to course work and some are just independent research.
I don't feel I need to explain myself to you, but since this was brought to my attention, I do believe you don't need to be concerned for anyone but yourself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.