Ok, so I must confess to the lack of reading skills: I started reviewing the code, then read the questions. So first the "code review comments" - which actually re-asks/answers your questions in part:
Code:
#define FIRSTNAME 0
#define MIDNAME 1
#define SURNAME 2
Why is this #define, and not an enum?
Code:
int typeofname = 0;
Not = FIRSTNAME? (Same other places?)
Code:
string_from_file[string_from_file.size()] = NULL;
Seems extremely fishy.
Code:
string_from_file.erase(0, 99);
Why not?
Code:
string_from_file.clear();
(It also works if your string should ever happen to be longer than 99, for example).
Code:
first_names.push_back(string_from_file);
middle_names.push_back(string_from_file);
last_names.push_back(string_from_file);
What exactly are you pushing back here, before you've read from the file?
Code:
pos = 0;
while (pos <= num_of_lines)
{
if (typeofname == FIRSTNAME)
cout << first_names[pos] << endl;
else if (typeofname == MIDNAME)
cout << middle_names[pos] << endl;
else if (typeofname == SURNAME)
cout << last_names[pos] << endl;
pos++;
}
Why not a for-loop.
And think about how many loop iterations you actually do here. Just walk through the code for example imagining that num_of_lines is 4. How many lines get printed, and how much data do you expect to have in such a case? [Answers Q1]
But overall: Why do you use getline, when it would make more sense to use the >> operator? That way, you could get rid of the entire while-loop in the middle of the code [the loop splitting the string] - which by the way could probably be simplified by just doing "charname[pos] != ' ' && charname[pos]" - if you reach end of string BEFORE you get to surname, it's probably wrong anyways, but currently, you don't even detect that scenario, and traipse on to the next line. If someone put "Matthew Mailer" (no middle name) in your file, you would then go on reading whatever rubbish happens to be after "\0" in memory - this will definitely lead to bad data output if it's not leading to a crash.
Further, is it part of your assignment to use three parallel arrays, rather than a single array containing a struct/class with first, middle and last name? [Sort of answer to Q2]
And on a pedantic note: perhaps you want to decide whether you use the term surname or last name - currently you have both in your code.
--
Mats