Skip to content

Instantly share code, notes, and snippets.

/helpmeplease.cc Secret

Created March 30, 2017 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3150303c8b51295198bd008b9465c2eb to your computer and use it in GitHub Desktop.
Save anonymous/3150303c8b51295198bd008b9465c2eb to your computer and use it in GitHub Desktop.
// NOTE: First line to be analyzed is:
// Matthew Alan Aberegg 1963 452,627
// among other things, I need to take this and put it in the format:
// Aberegg, Matthew Alan 1963 452,627
// The file i am opening is filled with lines like that one, except some are formatted correctly, some are not.
// The following (very incomplete) code SHOULD extract the last name, "Aberegg", but it seems to be caught in an
// infinite loop somewhere. Any help/suggestions for any improvement would be greatly appreciated.
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
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
//while(!fileIn.eof()){ // while the file is not at its end (COMMENTED THIS OUT IN HOPES OF GETTING IT WORKING FOR THE FIRST LINE ONLY)
getline(fileIn, currentLine);
for(count = 0; count < currentLine.length(); count++){
pos = currentLine.find_first_of(',');
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(" ")); // should be the last name of the person( i.e. "Aberegg")
}
count++;
}
}
}
// }
cout << name<<endl; // NEVER OUTPUTS :(
fileIn.close();
fileOut.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment