This is the program I'm currently working on for my homework. Basically, it changes the format of a name.
If input = Mayura Chen, output = Chen, Mayura
If input = Mayura Holy Chen, output = Chen, Mayura H.
(those are some random names ~_~) and this is the program I've done
fstream fin;
ostream fout;
fout.(outfile);
fin.open(file);
int count = 0;
while (getline(temp,fin) // this is set up to read until endline
{
// count spaces in string
for (loop through string)
if (temp[i] is " ")
count++;
// break the string up into 2 or three based on space count
for (int i = 0; i < temp.getlength(); i++)
until first " "
add to first
until second " "
add to second
if count == 2
until end
add third;
first = first_name;
if count is 1
{
second = last_name
}
if count is 2
{
second = middle_name
third = last_name
}
// display
fout last_name << "," << first_name;
if (count == 2)
fout << " " << middle_name[0] << ".";
fout << endl;
// erase for next time
first.erase();
second.erase();
third.erase();
count = 0;
}
fout.close();
fin.close();