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
Solved

Storing string arrays?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

860 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