Strings in arrays

This is a discussion on Strings in arrays within the C Programming forums, part of the General Programming Boards category; I'm just starting out and have an idea in mind to create a quiz program, not sure how to implement ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    Strings in arrays

    I'm just starting out and have an idea in mind to create a quiz program, not sure how to implement it yet because I don't understand how it would work. Also not sure if I'd have to use structures instead.

    Example:

    char questionanswer[][];

    Question 1. What is the capital of France?
    Answer 1. Paris

    Question 2. What is c?
    Answer 2: A language

    Both questions have different character lengths and different answer lengths, so how would you know how far to count in the array dynamically?

    It's not as if array element 1 was "What is the capital of France", array element 2 was "What is c?", which would be easy to set up since each question neatly fits into 1 array element, and so would an answer, which it make it easy to run through the questions, you'd just have a loop and match array element 1 in the question to array element 1 in the answer.

    Do people use arrays for this sort of thing or structures?

    I don't want anyone to write any code, I just want to know how it works, if it works, if you use arrays.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If you keep it super simple, you could just have an array of questions, an array of answers, and two integers to track how many questions they've gotten correct out of the total. Structures could be useful if you start tracking user profiles, etc...

    How you store the arrays in memory at run-time depends on how you store them before the program is run, I think. To save on memory you could store all the question/answer pairs in a file, and read them one at a time (maybe you'd even want to do some simple encryption in the file). You could read until a newline, and that would be the first question. Then you would read until the next new-line, and that would be the answer. Once you're done with that, record the results and discard the strings, then read the next pair. Of course, you could also have them all stored as constants in your program, but then you wouldn't need to declare space - just assign all the constants to pointers.

    At some point, you either need to set a limit and say "nothing can be longer than 80 characters", or you need to implement a more complex system of linked-lists, or something. To be that dynamic, you would probably need to use structs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 11:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 07:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21