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.
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.
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.