0

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;
}
Jake K
  • 25
  • 4
  • 2
    The immediate problem I see is that the while condition is `count2 < currentLine.length()` but inside the loop you only do `count2 = count2 - 2` so `count2` will always be smaller than `currentLine.length()`. At the end of the loop you increment `count`, did you mean to increment `count2` instead? – JJJ Mar 30 '17 at 05:28
  • It's commented out, but... [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Mar 30 '17 at 05:40
  • 1
    @JJJ Thanks so much, can't believe I missed that. – Jake K Mar 30 '17 at 05:51
  • @user4581301 Noted, I might be able to make that while loop include a static number since I'm only processing one file. Thanks! – Jake K Mar 30 '17 at 05:52
  • This is one of the reasons why it's important to use descriptive variable names. If you had for example `lineCount` and `characterCount` then it wouldn't be as easy to confuse them with each other. – JJJ Mar 30 '17 at 05:54
  • @JJJ makes sense, probably the exact type of comment my professor would make. Going to go back and rename some variables and fix up this project. Thanks again – Jake K Mar 30 '17 at 06:03

0 Answers0