array size defined by file.

Mar 23, 2015 at 12:58am
Is there a way to define an arrays size using a file? I have a file with 5 names and scores, I don't want to define a specific number to the array just in case I want to add more names and scores to it.

here is what I got so far.

1
2
3
4
5
6
7
8
9
int num;
string filename;
cin >> filename;
ifstream thename (filename);
vector<string> thenames[num];
vector<double> thescores[num];
  for (int p = 0; p < num; p++){
			inputname >> thenames[i]>>thescores[i];
		}
Mar 23, 2015 at 1:04am
Don't use an array. Use a std::vector:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector

Even better, use a std::map or a std::multimap depending on whether a player can have multiple scores or not.

Also, loop on the input operation rather than assuming in advance that you know how many scores are in the files.
Last edited on Mar 23, 2015 at 1:05am
Mar 23, 2015 at 1:10am
thanks
Topic archived. No new replies allowed.