Solved

Storing string arrays?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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++.

757 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

23 Experts available now in Live!

Get 1:1 Help Now