Solved

Storing string arrays?

Posted on 2003-10-28
3
294 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now