Thread: storing strings into a 2d array

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    storing strings into a 2d array

    Someone posted a question as to how a program could be written to ask questions, obtain an answer, and then compare it the user answer to the correct answer. I replied with a method of hard coding the questions and answers into an array, but thought about it in more detail. It might be useful to have an instructor be able to enter his or her questions and then the correct answer and store that info into a dat file. So I thought about that for awhile, but couldn't figure out exactly how to store the questions correctly... I got null pointer assignments. Anyway, I came up with something like

    Code:
    #include <stdio.h>
    
    #define MAXNUM 4 // four questions & answers
    #define MAXLEN 80 // max size of question string
    
    int main(void)
    {
      char *userQuestions[MAXNUM][MAXLEN];
      int userAnswers[MAXNUM]; // assume an int value for now.
    
      int i;
    
      for(i = 0; i < MAXNUM; i++)
      {
          printf("Enter question %d: ", i);
          // I debated over scanf or fgets but I'm not sure how
          // to read in the string
          scanf("%s", &userQuestions[i][MAXLEN]);
         
          printf("Enter the answer for question %d: ", i);
          scanf("%d", &userAnswers[i]);
        }
    
    ..... other code
    Any ideas as to how to approach this would be appreicated.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *userQuestions[MAXNUM][MAXLEN];
    This is basically an array of pointers, so you haven't allocated any memory for storing the strings.

    >>char userQuestions[MAXNUM][MAXLEN];
    would have been better.

    >>scanf("%s", &userQuestions[i][MAXLEN]);
    Use fgets(), its safer:
    >>fgets(userQuestions[i], MAXLEN, stdin);

    Check for a 0 length string to allow the user to terminate the input early.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Yet another problem that you've helped me to solve. Thanks Hammer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  2. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM
  3. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM