-
Manipulate a string?
I am reading data from a file into a vector. The data is in a form of lastname, firstname. I need to switch it to be firstname lastname. Any clues on how I could do this? I have the program doing everything it needs to do, but this. This is a minor part of the program, but it looks ugly in it's present form. Any help would be appreciated. Thank you!
-
Difficult to say without knowing how your vector is used and how you read the strings from file.
Code:
fs>> lastname >> firstname;
v.push_back(firstname);
v.push_back(lastname);
...perhaps?
>Thank you!
No no, thank you.
-
the data is a list in a file. the data is a GPA then lastname.firstname.
(example)
3.57 Jones, Steve
2.91 Kramer, Bill
there are 2 vectors, one for the GPA's and the second is for the string. the string ends up being lastname, firstname.
the final output needs to be in firstname lastname
Code:
void main ()
{
vector<double> gpa;
vector<string> names;
float f;
string s;
ifstream fin("input.txt");
if (!fin)
cerr << "error" << endl;
while (!fin.eof()) {
fin >> f;
gpa.push_back(f);
getline (fin, s);
names.push_back(s);
}
ofstream fout("output.txt");
fout << "Original Inputted Information\n\n";
for (int i = 0; i < gpa.size(); i++)
fout << gpa[i] << " " << names[i] << endl;
-
Code:
string firstname;
string lastname;
while (fin>> f) {
fin>> lastname >> firstname;
gpa.push_back(f);
names.push_back(firstname + " " + lastname);
}
Tada!
-
Thank you. That is almost what I need. It points me in the right direction. The problem is that the output should be in the original form before the names are reversed. Thank you for your help.