0

I'm trying to create a student database system for a school project. I'm trying to create a function that will search a .txt file for the student id and return all of the other variables on the string. This is working great if I search for the id of the student on the first line of the txt file but isn't capturing anything if I search for a student on another line. Am I missing something obvious?

The student data is 16 strings delimited by commas on each line. The student ID is the first string.

Thanks for any assistance!

StudentType findStudent(int studentToFind)
{
    ifstream inFile;
    inFile.open("students.txt");

    string currentLine;
    string dataRead[16];
    istringstream is;
    int currentStudent;

    if (inFile)
    {
        while (getline(inFile, currentLine))
        {
            is.str(currentLine);

            for (int i = 0; i < 16; i++)
            {
                getline(is, dataRead[i], ',');
            }

            currentStudent = stoi(dataRead[0]);

            if (currentStudent == studentToFind)
            {
                /*
                 Do stuff here
                */

                inFile.close();
                return foundStudent;
            }
            cin.ignore(); // Not sure if this is needed but I was trying to 
                          // clear the \n char if that was causing the issue
        }
    }
}
Dave Lea
  • 11
  • 2
  • Please don't just post your entire problem here. remove items not directly relevant to the issue / question you have. In this case, just about NONE of this code matters.... – UpAndAdam Mar 22 '16 at 20:03
  • 1
    Have you used a debugger (or print statements) to check your currentStudent and studentToFind values for each loop iteration to make sure they are what you expect? – Tuffwer Mar 22 '16 at 20:06
  • I've removed the parts where I assign all the values, sorry I assumed seeing the whole function would be helpful. I put a breakpoint in and it looks like the currentStudent value is never assigned to the value of the new line, so I'm assuming its not getting to the next line at all. – Dave Lea Mar 22 '16 at 20:12
  • Much better after the edit! good improvement – UpAndAdam Mar 22 '16 at 20:43
  • maybe a minimal example of a data file that demonstrates the problem. There appear to be some assumptions about the data file in your parsing logic, but it is hard to know if you are aware of them – Chip Grandits Mar 22 '16 at 20:46
  • also check out http://stackoverflow.com/questions/21814297/how-to-extract-mixed-format-using-istringstream and http://stackoverflow.com/questions/11719538/how-to-use-stringstream-to-separate-comma-separated-strings – UpAndAdam Mar 23 '16 at 14:41

1 Answers1

3

First : you aren't using cin, so get rid of cin.ignore().

Second : you should make sure you ALWAYS close infile at the end... so I would suggest not returning early or closing early, but using a break statement to exit your loop and then have a single return of whether you found it or not.

Third: Now that you removed all the 'gorp' we can finally hone in on the problem ... effectively the question is do we read all the lines?

Well let's check that, try printing out currentLine each time at the beginning of the while loop, if you know currentLine is updated properly, is is getting updated each time? yes...

ok then look at your next loop let's print out currentStudent each time... does currentStudent print the right value for each line? i.e. is the getline write into dataRead[i] actually writing what you think it should be to the right space?

Did you find the problem yet?

This is the kind of problem you need to learn how to solve yourself using print statements and a debugger. That what its for. If you are in visual studio run in debug mode and step through it... if not, use gdb. learn it and get used to it, you'll be using it a lot!

good luck

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • Thanks for the help! I was able to get this working by adding is.clear() before looping back into the next getline. My researching of .clear() is that it gets rid of any errors that are in the stream, any clue why it would be messed up in the first place? Any clue if its a best practice to clear the stream out like that? – Dave Lea Mar 23 '16 at 00:26
  • Again I would go through your example and figure out what you are doing wrong; at exactly what point is it NOT working.. That aside you need to issue a clear so that you can reuse the stream object after you have read all the data from it. Best practice is to either use a new object OR clear and then reload it. – UpAndAdam Mar 23 '16 at 14:40