So I have a programming project that is by far the most difficult one I've had to do thus far. We are given a file with a list of people's names, birthdays, and salaries. Among other things, we have to make sure each name is formatted in the correct way (LASTNAME, FIRST MIDDLE(if they have one))...Currently the following code is meant to extract their last name, assuming it is formatted incorrectly (FIRST MIDDLE LAST). The first line of the file is Matthew Alan Aberegg 1963 452,627 And the correct output would be Aberegg, Matthew Alan 1963 452,627
I have to do a few other things, but this is where I'm stuck. It is especially difficult because some names only have a first and last and only some are in the incorrect format. I commented out the first while loop, as I'm trying to get this to even work just for one name.
As a side note, the commented "cout" at the beginning is only shows up if i add an endl; to the end...otherwise it is just completely ignored. It seems like this is the case throughout the whole code. I really don't understand how that could be, either, but whatever, I have bigger problems than that.
Using Eclipse on a Mac, if that changes anything.
I'm sure there are points of misuse of cctype functions or ways I could shorten the code. Any suggestions are welcome! Thanks for any help, seriously, I've been stuck on this for way too long.
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
//cout << "test";
ifstream fileIn; // for input files
ofstream fileOut; // for output files
string currentLine; // for analyzing current line
fileIn.open("oldretirement.txt");
fileOut.open("newretirement.txt"); // TODO: move this closer to where it's needed
// TODO: make sure file opens first (while loop?)
int count = 0; // for parsing through each line
int count2 = 0; // for rearranging the name TODO: rename/is this needed?
int pos; // finds the position of first comma
string name; // for rearranging the name if not in correct format
bool rearranged = false; // for rearranging, true if the name is rearranged
//while(!fileIn.eof()){ // while the file is not at its end
getline(fileIn, currentLine);
//cout << currentLine;
for(count = 0; count < currentLine.length(); count++){
pos = currentLine.find_first_of(',');
// cout << pos<<endl;
if(isdigit(currentLine[pos-1])){
count2 = 0;
while(count2 < currentLine.length()){ // change this condition?
if(isdigit(currentLine[count2])){
cout << count2 <<endl; //test
count2 = count2 - 2; // goes back
while(!isspace(currentLine[count2])){
count--;
}
name = currentLine.substr(count2, currentLine.find(" "));
}
count++;
}
}
}
// }
fileIn.close();
fileOut.close();
return 0;
}