Solved

Storing string arrays?

Posted on 2003-10-28
3
301 Views
Last Modified: 2010-04-01
I have an a 1000 rows of data stored in a file something like this:

question 1 text       q1 answer          q1 difficulty
question 2 text       q2 answer          q2 difficulty
... (998 more)

After loading the data, where should I put this data?  I would like to put it in a multidimensional array, but am new to C++ data structures, and not sure if this is the best option.

What would be the best way to store this data in C++ so I can rapidly pull up a random question?
0
Comment
Question by:derekpapesch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
Mustak_Shaikh earned 50 total points
ID: 9640143
derekpapesh,

I think there are two option for this
1. Either go for structure or class
e.g. say first column is question identifier,
second as question_name and third as question description.
then create class as
class Question
{
    private:
   char question_ident[30];
   char question_name[30];
  char question_desc[30];
};
to fetch data from file go for this:
file>>objQuestion;
to write data on file:
file<<objQuestion;

2. two dimenional char array or pointer to pointer char e.g. char **strquestion.

regards,
Mustak_Shaikh
0
 
LVL 15

Expert Comment

by:efn
ID: 9640339
You should have a class that stores the data for one question and an array or vector of objects of that class.

If you are sure that there will always be 1000 questions, you can use an array.  A vector works like an array, but gives you more flexibility with its size--it's like an array that can grow as needed.

Mustak_Shaikh was on the right track recommending a structure or class, but you might want to represent the difficulty as a number rather than a string and you probably don't want all the members to be private.  You could also consider using string objects rather than character arrays.  As with vectors, string objects give you more flexibility with the size.

--efn
0
 
LVL 4

Author Comment

by:derekpapesch
ID: 9640344
Thanks Mustak_Shaikh,

That's brilliant.  It's working.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question