I'm trying to read data from a file using dynamic arrays, everything is great now except for one thing:

if I have a file with lets say 20 students' IDs and their scores
before starting the reading proccess, I did this:

string *studentID;
double *testScore;
cout<<"Enter number of student: ";
cin>>numberOfStudents;
cout<<endl;
studentID=new string[numberOfStudents];
testScore=new double[numberOfStudents];

which is illogical, the program must now the number of students based on the number o students' IDs saved in the infile.

can this be done without letting the user enter it?

thank you in advanced, and I hope I'm not breaking any rules.

Recommended Answers

All 3 Replies

Perhaps the first number stored in the data file could be the number of students?

Eg.
2
Fred Flintstone
Barney Rubble

Or use a vector, which you can add to arbitrarily
std::vector< std::string > studentID;

fin >> aStudent;
studentID.push_back( aStudent );

Perhaps the first number stored in the data file could be the number of students?

Eg.
2
Fred Flintstone
Barney Rubble

Or use a vector, which you can add to arbitrarily
std::vector< std::string > studentID;

fin >> aStudent;
studentID.push_back( aStudent );

there's no number heading the file indicating number of students, but thank you I might use it if I couldn't find some other way.

for using vectors, I have no idea what vectors are. we haven't used them anytime before, I tried adding your lines to the code but it keeps on giving me errors.

Thank you again : )

Or use a vector, which you can add to arbitrarily
std::vector< std::string > studentID;

fin >> aStudent;
studentID.push_back( aStudent );

You need to add std::string aStudent;

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.