Question RE: Arrays, Rand, & Pointers 
Author Message
 Question RE: Arrays, Rand, & Pointers

Hi Everyone,

I'm taking a C programming class right now and am stumped as to how to
proceed with this program.  Any help would be GREATLY appreciated.
Basically, I need to write a quiz program that uses an array to declare my
questions and a corresponding array that holds the correct multiple guess
answer.  I need to do this using both standard array processing using
subscripts as well as with array processing using pointers.  A header file
will also need to be created.  Finally, a random element should be present.
Here is the basic quiz program that I have to work with...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void StateCapitalQuestions(int *player1score, int *player2score);

void FishingQuestions(int *player1score, int *player2score);

void FootballQuestions(int *player1score, int *player2score);

void main (void)
{

   int   player1score=0;
   char  player1name[81];
   int   player2score=0;
   char  player2name[81];

   char  inputanswer[81];

   printf("\n\n\t\t++Millionare multiple choice Quiz program++\n\n");

   printf("\nPlayer 1 enter your name? ");
   scanf("%s",player1name);
   printf("\nPlayer 2 enter your name? ");
   scanf("%s",player2name);

   StateCapitalQuestions(&player1score, &player2score);

   FishingQuestions(&player1score, &player2score);

   FootballQuestions(&player1score, &player2score);

   printf("\n\nPlayer 1 score: %d, Player 2 score: %d\n", player1score,
player2score);

   if (player1score > player2score)
   {
      printf("\n\nPlayer 1: %s your final millionare question is: Who shoot
JFK?\n", player1name);
      scanf("%s",inputanswer);
      if (strlen(inputanswer) > 2)
      {
         printf("\n\nPlayer 1: %s you have won one million dollars!\n",
player1name);
      }
      else
      {
         printf("\n\nPlayer 1: %s sorry you have lost.  Play again soon.\n",
player1name);
      }
   }
   else if (player1score < player2score) /* player 2 won */
   {
      printf("\n\nPlayer 2: %s your final millionare question is: Who shoot
JFK?\n", player2name);
      scanf("%s",inputanswer);
      if (strlen(inputanswer) > 2)
      {
         printf("\n\nPlayer 2: %s you have won one million dollars!\n",
player2name);
      }
      else
      {
         printf("\n\nPlayer 2: %s sorry you have lost.  Play again soon.\n",
player2name);
      }
   }
   else /* oh no the games a draw */
      printf("\n\nThe game is a draw.  No winners.  Play again soon.\n",
player2name);

Quote:
}

void StateCapitalQuestions(int *player1score, int *player2score)
{  

    char  inputanswer[81];                      

    printf("\n\n\t\t*State Capitals Millionare qualifying questions*\n");

 /* Question 1 */  
    printf("\n\nPlayer 1 ->");
    printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'C')
        *player1score += 1;
    else
    {
       printf("\nSorry Player 1, wrong");
       printf("\nPlayer 2 how about you?");
       printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
       scanf("%s",inputanswer);
       if (toupper(inputanswer[0]) == 'C')
       {
           *player2score +=1;
           printf("\nCorrect Player 2");
       }                                
       else
           printf("\nSorry Player 2, wrong");
    }

    /* Question 2 */
    printf("\n\nPlayer 2 ->");
    printf("\n2. What is the capital of Maine?\n A. Augusta, B. Portland, C.Westbrook\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'A')
        *player2score += 1;
    else
    {

   printf("\nSorry Player 2, wrong");
        printf("\nPlayer 1 how about you?");
        printf("\n2. What is the capital of Maine?\n A. Augusta, B.
Portland, C. Westbrook\n");
        scanf("%s",inputanswer);
        if (toupper(inputanswer[0]) == 'A')
        {
           *player1score +=1;
           printf("\nCorrect Player 1");
        }
        else
           printf("\nSorry Player 1, wrong");
    }

    /* Question 3 */
    printf("\n\nPlayer 1 ->");
    printf("\n3. What is the capital of New Hampshire?\n A. Salford,
B.Trenton, C. Concord\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'C')
        *player1score += 1;
    else
    {
       printf("\nSorry Player 1, wrong");
       printf("\nPlayer 2 how about you?");
       printf("\n3. What is the capital of New Hampshire?\n A. Salford,
B.Trenton, C. Concord\n");
       scanf("%s",inputanswer);
       if (toupper(inputanswer[0]) == 'C')
       {
           *player2score +=1;
           printf("\nCorrect Player 2");
       }
       else
           printf("\nSorry Player 2, wrong");
    }

    /* Question 4 */
    printf("\n\nPlayer 2 ->");
    printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
C.Dayton\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'A')
        *player2score += 1;
    else
    {
        printf("\nSorry Player 2, wrong");
        printf("\nPlayer 1 how about you?");
        printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
C.Dayton\n");
        scanf("%s",inputanswer);
        if (toupper(inputanswer[0]) == 'A')
        {
           *player1score +=1;
           printf("\nCorrect Player 1");
        }
        else
           printf("\nSorry Player 1, wrong");
    }

    /* Question 5 */
    printf("\n\nPlayer 1 ->");
    printf("\n5. What is the capital of Wisconsin?\n A. Albany, B.C{*filter*}te,
C. Madison\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'C')
        *player1score += 1;
    else
    {
       printf("\nSorry Player 1, wrong");
       printf("\nPlayer 2 how about you?");
       printf("\n5. What is the capital of Wisconsin?\n A. Albany,
B.C{*filter*}te, C. Madison\n");
       scanf("%s",inputanswer);
       if (toupper(inputanswer[0]) == 'C')
       {
           *player2score +=1;
           printf("\nCorrect Player 2");
       }
       else
           printf("\nSorry Player 2, wrong");
    }

    /* Question 6 */
    printf("\n\nPlayer 2 ->");
    printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St. Paul,
C. Montgomery\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'A')
        *player2score += 1;
    else
    {
        printf("\nSorry Player 2, wrong");
        printf("\nPlayer 1 how about you?");
        printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St.
Paul, C. Montgomery\n");
        scanf("%s",inputanswer);
        if (toupper(inputanswer[0]) == 'A')
        {
           *player1score +=1;
           printf("\nCorrect Player 1");
        }
        else
           printf("\nSorry Player 1, wrong");
    }

    /* Question 7 */
    printf("\n\nPlayer 1 ->");
    printf("\n7. What is the capital of North Carolina?\n A. Raleigh, B.
C{*filter*}te, C. Kaiser\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'B')
        *player1score += 1;
    else
    {
       printf("\nSorry Player 1, wrong");
       printf("\nPlayer 2 how about you?");
       printf("\n7. What is the capital of North Carolina?\n A. Raleigh, B.
C{*filter*}te, C. Kaiser\n");
       scanf("%s",inputanswer);
       if (toupper(inputanswer[0]) == 'B')
       {
           *player2score +=1;
           printf("\nCorrect Player 2");
       }
       else
           printf("\nSorry Player 2, wrong");
    }

    /* Question 8 */
    printf("\n\nPlayer 2 ->");
    printf("\n8. What is the capital of Florida?\n A. Baton Rouge, B.
Tallahassee, C. Alderney\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'B')
        *player2score += 1;
    else
    {
        printf("\nSorry Player 2, wrong");
        printf("\nPlayer 1 how about you?");
        printf("\n8. What is the capital of Florida?\n A. Baton Rouge, B.
Tallahassee, C. Alderney\n");
        scanf("%s",inputanswer);
        if (toupper(inputanswer[0]) == 'B')
        {
           *player1score +=1;
           printf("\nCorrect Player 1");
        }
        else
           printf("\nSorry Player 1, wrong");
    }

    /* Question 9 */
    printf("\n\nPlayer 1 ->");
    printf("\n9. What is the capital of Illinois?\n A. Dresden, B.
Montpelier, C. Springfield\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'C')
        *player1score += 1;
    else
    {
       printf("\nSorry Player 1, wrong");
       printf("\nPlayer 2 how about you?");
       printf("\n9. What is the capital of Illinois?\n A. Dresden, B.
Montpelier, C. Springfield\n");
       scanf("%s",inputanswer);
       if (toupper(inputanswer[0]) == 'C')
       {
           *player2score +=1;
           printf("\nCorrect Player 2");
       }
       else
           printf("\nSorry Player 2, wrong");
    }

    /* Question 10 */
    printf("\n\nPlayer 2 ->");
    printf("\n10. What is the capital of Vermont?\n A. Montpelier, B. Dover,
C. Amarillo\n");
    scanf("%s",inputanswer);
    if (toupper(inputanswer[0]) == 'A')
        *player2score += 1;
    else
    {
        printf("\nSorry Player 2, wrong");
        printf("\nPlayer 1 how about you?");
        printf("\n10. What is the capital of Vermont?\n A. Montpelier, B.
Dover, C. Amarillo\n");
        scanf("%s",inputanswer);
        if (toupper(inputanswer[0]) == 'A')
        {
           *player1score +=1;
           printf("\nCorrect Player 1");
        }
        else
           printf("\nSorry Player 1, wrong");
    }

Quote:
}

void FishingQuestions(int *player1score, int *player2score)
{  

    char  inputanswer[81];                      

    /* This is where the fishing questions go */

Quote:
}                                

void FootballQuestions(int *player1score, int *player2score)
{  

    char  inputanswer[81];                      

    /* This is where the football questions go */

Quote:
}



Thu, 11 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers

Quote:

> Hi Everyone,

> I'm taking a C programming class right now and am stumped as to how to
> proceed with this program.  Any help would be GREATLY appreciated.
> Basically, I need to write a quiz program that uses an array to declare my
> questions and a corresponding array that holds the correct multiple guess
> answer.  I need to do this using both standard array processing using
> subscripts as well as with array processing using pointers.  A header file
> will also need to be created.  Finally, a random element should be present.
> Here is the basic quiz program that I have to work with...

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

You need to include <ctype.h> if you plan to use the toupper()
function/macro.

Quote:

> void StateCapitalQuestions(int *player1score, int *player2score);

> void FishingQuestions(int *player1score, int *player2score);

> void FootballQuestions(int *player1score, int *player2score);

> void main (void)

main returns an int.

Quote:
> {

>    int   player1score=0;
>    char  player1name[81];
>    int   player2score=0;
>    char  player2name[81];

>    char  inputanswer[81];

>    printf("\n\n\t\t++Millionare multiple choice Quiz program++\n\n");

>    printf("\nPlayer 1 enter your name? ");
>    scanf("%s",player1name);
>    printf("\nPlayer 2 enter your name? ");
>    scanf("%s",player2name);

scanf() is potentially dangerous (see the faq). fgets(buffer, sizeof
buffer, stdin); is safer, but you need to ensure that the full input
stream is handled correctly in each call. This applies to the remainder
of your program too.

- Show quoted text -

Quote:

>    StateCapitalQuestions(&player1score, &player2score);

>    FishingQuestions(&player1score, &player2score);

>    FootballQuestions(&player1score, &player2score);

>    printf("\n\nPlayer 1 score: %d, Player 2 score: %d\n", player1score,
> player2score);

>    if (player1score > player2score)
>    {
>       printf("\n\nPlayer 1: %s your final millionare question is: Who shoot
> JFK?\n", player1name);
>       scanf("%s",inputanswer);
>       if (strlen(inputanswer) > 2)

Hmmm.... Unless you're convinced that William Shakespeare shot JFK,
you'd better replace that strlen() answer-checking operation with a
strcmp() !

- Show quoted text -

Quote:
>       {
>          printf("\n\nPlayer 1: %s you have won one million dollars!\n",
> player1name);
>       }
>       else
>       {
>          printf("\n\nPlayer 1: %s sorry you have lost.  Play again soon.\n",
> player1name);
>       }
>    }
>    else if (player1score < player2score) /* player 2 won */
>    {
>       printf("\n\nPlayer 2: %s your final millionare question is: Who shoot
> JFK?\n", player2name);
>       scanf("%s",inputanswer);
>       if (strlen(inputanswer) > 2)
>       {
>          printf("\n\nPlayer 2: %s you have won one million dollars!\n",
> player2name);
>       }
>       else
>       {
>          printf("\n\nPlayer 2: %s sorry you have lost.  Play again soon.\n",
> player2name);
>       }
>    }
>    else /* oh no the games a draw */
>       printf("\n\nThe game is a draw.  No winners.  Play again soon.\n",
> player2name);

> }

Consider:

if(player1score != player2score) {
    char *winner = player1score > player2score ? player1name :
player2name;
    printf("%s: Your million dollar question: Who shot JFK? ", winner);
    fflush(stdout);
    fgets(inputanswer, sizeof inputanswer, stdin);
    /* case conversion function call */
    if(!strcmp("CIA", inputanswer))
        printf("%s: You have won a million dollars!\n", winner);
    else
        printf("%s: Sorry you have lost. Play again soon\n", winner);

Quote:
}

else printf("Drawn game\n");

Probably not as optimal as it can be, but it makes it a little more
compact and safer.

And don't forget int main(void) { ..... return 0; }

Quote:
> void StateCapitalQuestions(int *player1score, int *player2score)
> {

>     char  inputanswer[81];

This doesn't need to be an array. A single char will suffice.

Quote:

>     printf("\n\n\t\t*State Capitals Millionare qualifying questions*\n");

>  /* Question 1 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
>     scanf("%s",inputanswer);

getchar() is better (sort of).

- Show quoted text -

Quote:
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

Quite a moot point, but these routines could be made more `symmetrical`
by notifying player1 of a _correct_ answer.

- Show quoted text -

Quote:
>     /* Question 2 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n2. What is the capital of Maine?\n A. Augusta, B. Portland, C.Westbrook\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {

>    printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n2. What is the capital of Maine?\n A. Augusta, B.
> Portland, C. Westbrook\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

There's a lot of repetition here. Some of these strings could be placed
in explicit arrays and used in an standard output function. If you think
about it, I'm sure you could cut your function down to half its size.

As for your required randomizing element, a possible thing to do is to
create an array of structures, each structure belonging to a single
question and its associated correct answer. The element of the array can
be randomized with a basic modulus based rand() call (<stdlib.h>). The
faq has a few suggestions for appropriate randomizing statements. In
this way, a random structure is referenced and the question can be drawn
from it very easily.

typedef struct {
    char *q;
    char correct;

Quote:
}question_t;

question_t states[NUM_QUESTIONS];
question_t fishing[NUM_QUESTIONS];
question_t football[NUM_QUESTIONS];
...
...
...

What to do with this? Well, you mentioned that you needed a header file,
so this is a good place to define some symbolic constants and declare
some structures to control your program's data. I suppose you could also
place your function prototypes in there, and maybe make the shared
information (such as names and scores) global variables.

Have fun,

Mitch.

- Show quoted text -

Quote:

>     /* Question 3 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n3. What is the capital of New Hampshire?\n A. Salford,
> B.Trenton, C. Concord\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n3. What is the capital of New Hampshire?\n A. Salford,
> B.Trenton, C. Concord\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 4 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
> C.Dayton\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
> C.Dayton\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 5 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n5. What is the capital of Wisconsin?\n A. Albany, B.C{*filter*}te,
> C. Madison\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n5. What is the capital of Wisconsin?\n A. Albany,
> B.C{*filter*}te, C. Madison\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 6 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St. Paul,
> C. Montgomery\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St.
> Paul, C. Montgomery\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 7 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n7. What is the capital of North Carolina?\n A. Raleigh, B.
> C{*filter*}te, C. Kaiser\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'B')
>         *player1score += 1;

...

read more »



Fri, 12 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers


1) Input: scanf("%s", buff) is no better than gets(). In my opinion gets() is
a BAD THING and in everyone elses it is a VERY BAD THING - we are having a
nice debate on the issue in a neighbouring thread.

I recommend

char *getline(FILE *fp)
{
  char *answer;
  int buffsize = 1024;
  int nread = 0;

  answer = malloc(1024);
  if(!answer)
    exit(EXIT_FAILURE);

  do
  {
    ch = fgetc(fp);
    answer[nread] = ch;
    nread++;
    if(nread > buffsize)
    {
       buffsize += 1024;
       answer = realloc(answer, buffsize);
       if(!answer)
         exit(EXIT_FAILURE);
    }
  } while(ch != '\n' || ch != EOF);

  answer[nread-1] = 0; /* overwrite '\n' or EOF will NUL */

  return answer;

Quote:
}

This is untested so test on long input. If the OS won't give you a few K of
memory to hold an input you might as well exit() IMHO, but you might like to
return 0 on this condition to handle it more gracefully.

Functions: these are not well designed. Bascially functions should be pure
functions if possible (that is, they should take all their input as
parameters). The exception is when input is user-supplied.

You need several parallel arrays

char **fishing_questions;  /* fill this array first thing, probably from a
file */

char *fishing_answers;  /* ditto. Ensure answers are kept in synch with
questions */

Also arrays of other types of questions.

Now you need a function like this.

/* ask a question. Return TRUE (1) if the answer is right, FALSE (0) if wrong
*/ int question(char **list, char *answers, int N) {  int index;  char
*answer;

  index = (rand >> 2) % N;    /* icky. Don't use the bottom bits of rand() */

  printf("%s\n", list[index]);
  answer = getline(stdin);    /* get a line from the user */

  /* the guts of the function go here. is the answer right or wrong ? */
  free(answer);

  return is_it_right_or_wrong;

Quote:
}

> void StateCapitalQuestions(int *player1score, int *player2score)
> {

>     char  inputanswer[81];

>     printf("\n\n\t\t*State Capitals Millionare qualifying questions*\n");

>  /* Question 1 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n1. What is the capital of Pennsylvania?\n A. Philadelphia, B.Scranton, C. Harrisburg\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 2 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n2. What is the capital of Maine?\n A. Augusta, B. Portland, C.Westbrook\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {

>    printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n2. What is the capital of Maine?\n A. Augusta, B.
> Portland, C. Westbrook\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 3 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n3. What is the capital of New Hampshire?\n A. Salford,
> B.Trenton, C. Concord\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n3. What is the capital of New Hampshire?\n A. Salford,
> B.Trenton, C. Concord\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 4 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
> C.Dayton\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n4. What is the capital of Iowa?\n A. Des Moines, B. Dover,
> C.Dayton\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 5 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n5. What is the capital of Wisconsin?\n A. Albany, B.C{*filter*}te,
> C. Madison\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n5. What is the capital of Wisconsin?\n A. Albany,
> B.C{*filter*}te, C. Madison\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 6 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St. Paul,
> C. Montgomery\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n6. What is the capital of Arizona?\n A. Phoenix, B. St.
> Paul, C. Montgomery\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 7 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n7. What is the capital of North Carolina?\n A. Raleigh, B.
> C{*filter*}te, C. Kaiser\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'B')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n7. What is the capital of North Carolina?\n A. Raleigh, B.
> C{*filter*}te, C. Kaiser\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'B')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 8 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n8. What is the capital of Florida?\n A. Baton Rouge, B.
> Tallahassee, C. Alderney\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'B')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n8. What is the capital of Florida?\n A. Baton Rouge, B.
> Tallahassee, C. Alderney\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'B')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

>     /* Question 9 */
>     printf("\n\nPlayer 1 ->");
>     printf("\n9. What is the capital of Illinois?\n A. Dresden, B.
> Montpelier, C. Springfield\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'C')
>         *player1score += 1;
>     else
>     {
>        printf("\nSorry Player 1, wrong");
>        printf("\nPlayer 2 how about you?");
>        printf("\n9. What is the capital of Illinois?\n A. Dresden, B.
> Montpelier, C. Springfield\n");
>        scanf("%s",inputanswer);
>        if (toupper(inputanswer[0]) == 'C')
>        {
>            *player2score +=1;
>            printf("\nCorrect Player 2");
>        }
>        else
>            printf("\nSorry Player 2, wrong");
>     }

>     /* Question 10 */
>     printf("\n\nPlayer 2 ->");
>     printf("\n10. What is the capital of Vermont?\n A. Montpelier, B. Dover,
> C. Amarillo\n");
>     scanf("%s",inputanswer);
>     if (toupper(inputanswer[0]) == 'A')
>         *player2score += 1;
>     else
>     {
>         printf("\nSorry Player 2, wrong");
>         printf("\nPlayer 1 how about you?");
>         printf("\n10. What is the capital of Vermont?\n A. Montpelier, B.
> Dover, C. Amarillo\n");
>         scanf("%s",inputanswer);
>         if (toupper(inputanswer[0]) == 'A')
>         {
>            *player1score +=1;
>            printf("\nCorrect Player 1");
>         }
>         else
>            printf("\nSorry Player 1, wrong");
>     }

> }

> void FishingQuestions(int *player1score, int *player2score)
> {

>     char  inputanswer[81];

>     /* This is where the fishing questions go */

> }

> void FootballQuestions(int *player1score, int *player2score)
> {

>     char  inputanswer[81];

>     /* This is where the football questions go */

> }

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Fri, 12 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers


Quote:


> 1) Input: scanf("%s", buff) is no better than gets().

I agree.

Quote:
> In my opinion gets() is
> a BAD THING and in everyone elses it is a VERY BAD THING - we are having
a
> nice debate on the issue in a neighbouring thread.

Indeed. :-)

Quote:

> I recommend

I don't. See below for why...

Quote:

> char *getline(FILE *fp)
> {
>   char *answer;
>   int buffsize = 1024;
>   int nread = 0;

>   answer = malloc(1024);
>   if(!answer)
>     exit(EXIT_FAILURE);

This seems a little extreme.

Quote:

>   do
>   {
>     ch = fgetc(fp);
>     answer[nread] = ch;
>     nread++;
>     if(nread > buffsize)
>     {
>        buffsize += 1024;
>        answer = realloc(answer, buffsize);

This is not the best way to use realloc. Define a spare pointer, and use it
to catch the return value of realloc, so that (in the event of failure) you
don't lose the original pointer. Of course, in this case, it doesn't matter
because you immediately

Quote:
>        if(!answer)
>          exit(EXIT_FAILURE);

which I still think is a little extreme. After all, this is only a
line-reading function. Why introduce this extra point of failure in such a
simple program? I recommend fgets() instead.

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

comp.lang.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 12 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers
: I recommend

: char *getline(FILE *fp)
: {
:   char *answer;
:   int buffsize = 1024;
:   int nread = 0;

Here is some bug fixes to your code:
    int ch;          /* declare variable 'ch' for fgetc() */

:   answer = malloc(1024);
:   if(!answer)
:     exit(EXIT_FAILURE);

:   do
:   {
:     ch = fgetc(fp);
:     answer[nread] = ch;
:     nread++;

:     if(nread > buffsize)
:     {
:        buffsize += 1024;
:        answer = realloc(answer, buffsize);
:        if(!answer)
:          exit(EXIT_FAILURE);
:     }

This use of realloc() isn't the best.  Use a temporary variable to guard
against losing answer in the case that realloc() returns NULL.  So,

      if( nread > buffsize ) {
          char *tmp;
          buffsize += 1024
          tmp = realloc( answer, buffsize );
          if( !tmp ) {
             fprintf( stderr,
                      "realloc() failed trying to reallocate %d bytes.\n",
                       buffsize );
             break;
          }
          else
             answer = tmp;
      }

:   } while(ch != '\n' || ch != EOF);

:   answer[nread-1] = 0; /* overwrite '\n' or EOF will NUL */

:   return answer;
: }

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Fri, 12 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers


Quote:
> Of course, in this case, it doesn't matter
> because you immediately

> >        if(!answer)
> >          exit(EXIT_FAILURE);

> which I still think is a little extreme. After all, this is only a
> line-reading function. Why introduce this extra point of failure in such a
> simple program? I recommend fgets() instead.

In fact the program won't exit with EXIT_FAILURE unless fed a maliciously
long line, or very short of memory. If so short of memory it is unlikely to
be able to proceed normally in any event.

You admitted that correct use of fgets() was "fiddly" and declined to produce
an example. I do not doubt that such a task is within your ability, but
consider that the routine will probably have to be re-written several times
(sometimes valid input is bounded, sometimes unbounded, sometimes the calling
routine can handle partial lines, sometimes the buffer has to grow
dynamically). getline() on the other hand needs to be written once, and is
useful any time you want to read a line of ascii characters from an input
stream.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 13 Jul 2002 03:00:00 GMT  
 Question RE: Arrays, Rand, & Pointers


getline() on the other hand needs to be written once, and is

Quote:
> useful any time you want to read a line of ascii characters from an input
> stream.

Well, I'll happily concede that it's a massive improvement on gets(). ;-)


Sat, 13 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Question about character arrays & pointers

2. Difference between pointers & arrays - A question

3. probably an easy array, rand() question

4. qsorting & managing struct arrays, pointer arrays

5. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

6. Array of pointers, pointer to array...

7. array pointer/pointer array

8. arrays pointers array of pointers

9. arrays of strings & possible pointer arithmetic

10. Pointers & Arrays

11. Help on basic pointers & arrays

12. segmentation fault - pointer & array

 

 
Powered by phpBB® Forum Software