Jump to content

The Question Answer Thinger


Recommended Posts


#include <stdio.h>
#include <conio.h>

#include "qa.h"

int main(char **argv, int argc) {
struct LinkedList list;
int choice;

// zero memory
memset(&list, 0, sizeof(struct LinkedList));

       // how to use functions in qa.c
       // print a question!!!
Question("Hello World?");
       // add answers to a very simple linked list
AddAnswer(&list, "No");
AddAnswer(&list, "Yes");
AddAnswer(&list, "Grapefruit");
       // display answers in list and return the user choice. the true specifies whether to free() each answer after displaying it or not.
choice = Answer(&list, TRUE);

printf("Option %d selected\n", choice);


return 0;



#include "qa.h"

void Question(char *msg) {
   int strLen = strlen(msg);
   int i;

   for (i = 0; i < strLen; i++)


   for (i = 0; i < strLen; i++)

void AddAnswer(struct LinkedList *list, char *anAnswer) {
// pointless really, but it gives me an excuse to have linkedlist.c
AddToList(list, anAnswer);

int Answer(struct LinkedList *list, BOOLEAN autoFree) {
   int i, choice = 0;
   BOOLEAN choiceValid = TRUE;
   // don't use `static char *input` here if you free it at the end of the 
   // function. i'm a stupid dumb arse!!!!
   char *input = (char *)malloc(3 * sizeof(char));

   // check malloc for input buffer
   assert(input != NULL);
   memset(input, 0, 3 * sizeof(char));

   // test if the list has been initialized
   if (list->head != NULL) {
       // loop through list
       list->curr = list->head;
       i = 1;
       while (list->curr) {
           printf("%d. %s\n", i, list->curr->data);

 	if (autoFree) {
           	list->tmp = list->curr;

           list->curr = list->curr->next;

 	if (autoFree) {
               if (list->tmp != NULL) {


       printf("\n> ");

       // user choice
       do {
           if (!choiceValid) {
               printf("(Invalid) > ");

           fgets(input, 3, stdin);
           choice = atoi(input);
           choiceValid = FALSE;
       } while (choice < 1 || choice > list->length);


       // reset list for next answers
 if (autoFree) {
           // dont free head here either, retard. it's already been free'd in the
           // while loop
       	memset(list, 0, sizeof(struct LinkedList));
   } else {
       printf("ERROR: Answers haven't been initialized\n");

   return choice;



#ifndef QA_H
#define QA_H

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
#include "linkedlist.h"

typedef enum {FALSE = 0, TRUE = 1} BOOLEAN;

struct Answers {
   int next;
   int max;
   char **answers;

void Question(char *);
int Answer(struct LinkedList *, BOOLEAN);
void AddAnswer(struct LinkedList *, char *);




#include "linkedlist.h"

void AddToList(struct LinkedList *list, char *data) {
   if (list->head == NULL) {
       list->head = (struct LinkedListNode *)calloc(1, sizeof(struct LinkedListNode));
       assert(list->head != NULL);

       memset(list->head, 0, sizeof(struct LinkedListNode));

       list->tail = list->head;
       list->tail->data = data;
   } else {
       list->tail->next = (struct LinkedListNode *)calloc(1, sizeof(struct LinkedListNode));
       assert(list->tail->next != NULL);

       memset(list->tail->next, 0, sizeof(struct LinkedListNode));

       list->tail = list->tail->next;
       list->tail->data = data;





#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>

struct LinkedListNode {
   char *data;
   struct LinkedListNode *next;

struct LinkedList {
   struct LinkedListNode *curr;
   struct LinkedListNode *head;
   struct LinkedListNode *tail;
   struct LinkedListNode *tmp;
   int length;

void AddToList(struct LinkedList *, char *);



I hate having to zero the memory to begin with, but i can't figure a way round that. Might be handy for menu's (that's what I used it for). See main() for usage..


Any pointers or... suggestions how to do it all better... orrr whatever, i'd appreciate it. I can't help but think it's a giant mess that just happens to work reasonably well :lol:

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...