I am trying to figure out the best way to create a dynamic array/vector (not sure which) that would take in a string and double and store it. I then need to print the string with the corresponding double into a list and produce a total for all the doubles that were entered. I am lost as to how I set up my header and source file so that I can accomplish this. I am using C++ How to Program by Deitel, I have read through chapter 7 (arrays and vectors) and chapter 22 (STL on vectors) but I am still lost on how to begin this. I know I will need to loop the vector with a while loop so that if the user enters 0 or NULL it will terminate for prompting the user to enter in a string and a double. If someone could help by providing me an example of what this should be set up like I would greatly appreciate it. I need to use this to create a list of stocks and amounts so I am not looking for anyone to do my homework. But I just want to see how a vector can be used to take in a string and a double and later retrieve it for outputting a list and a total. Thanks in advance.

Create a structure (or a class) that contains your string and double and then make a vector of those.

You can use a normal loop or one of the stl algorithms to perform operations on it.

struct mypair
{
string name;
double value;
};

int main()
{
struct mypair a;

a.name = "test";
a.value = 1200;

vector test;
test.push_back(a);

while (test.at(0).name != NULL || test[0].value != 0)
{
cout << "Name is " << test.at(0).name << ", Value is " << test[0].value << endl;
}

return 0;
}

Something like this? Does the struct go in my .cpp or in .h? I would assume I could include my struct in a function's member definition that I declare, right?

If you want to get the most "reuse" possible out of the struct you could put it in a header file by itself coupling it with an empty cpp file with the same name as the header file, but since the struct is so straightforward leaving it like it is is fine, IMO.

You need to have some header files included at the start of the program, but assuming they were left out on purpose, then you have a good start.

In C++ you only use the keyword struct when you are declaring a class. It need not be used when declaring an instance of (object of) the class like you do on line 10.

If you are going to use STL containers/etc, then you need some way to access namespace std;. Your technique to do so may have been left out of the post on purpose, though.

When declaring vectors or other objects of a templated class you have to tell the compiler what type the vector (or other container) will hold. So it would be:

vector<mypair> test;

on line 15.

Your loop is a little out of kilter. Try something more like this:

input information into a
while a.name not NULL or a.value not zero
  push a onto test
  get another set of values for a
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.