I'm currently writing a program where it'll display the student records in 4 columns(ID, first name, last name, and GPA), sort their records in ascending order, and reset each of their GPA to 0.0 from the sorted student records.
The student records are stored in a text file I called "StudentInfo.txt"
1 2 3 4 5 6
123456789 John Johnson 3.5
512434990 Mary Jackson 3.9
342432444 Peter Young 2.3
470068625 Jim Lee 2.9
234324324 Tammy Gaddis 3.1
121219000 Ester Schwab 2.7
I've done most of the code, but the only thing I'm having trouble with is outputting the sorted student records and the reset GPA. If anyone can help me out on this, I would appreciate it!
ID: Name: GPA:
___________________________________
123456789 John Johnson 3.5
512434990 Mary Jackson 3.9
342432444 Peter Young 2.3
470068625 Jim Lee 2.9
234324324 Tammy Gaddis 3.1
121219000 Ester Schwab 2.7
___________________________________
Sorting Students by ID...
Resetting GPA Data...
ID: Name: GPA:
___________________________________
123456789 John Johnson 3.5
512434990 Mary Jackson 3.9
342432444 Peter Young 2.3
470068625 Jim Lee 2.9
234324324 Tammy Gaddis 3.1
121219000 Ester Schwab 2.7
___________________________________
Sorting Students by ID...
ID: Name: GPA:
___________________________________
512434990 Mary Jackson 3.9
123456789 John Johnson 3.5
234324324 Tammy Gaddis 3.1
470068625 Jim Lee 2.9
121219000 Ester Schwab 2.7
342432444 Peter Young 2.3
___________________________________
Resetting GPA Data...
ID: Name: GPA:
___________________________________
512434990 Mary Jackson 0.0
123456789 John Johnson 0.0
234324324 Tammy Gaddis 0.0
470068625 Jim Lee 0.0
121219000 Ester Schwab 0.0
342432444 Peter Young 0.0
___________________________________
What you are pointing to won't live longer than the while() loop it is defined in. Just use straightfoward structs.
Don't use .eof() to test for no more records. This won't turn true until after you've tried to read the data ... by which time it will be too late and you'll end up with (here) duplicate records.
As @jlb says, your input is obviously in the wrong place.
@jlb and @lastchance; I replaced my code here that actually compiles and reproduces the problem, which is outputting the sorted student records and their reset GPA.
@Orion98
- When you change your code please put any revised version BELOW, or the thread will cease to make sense.
- Please make the changes we suggested and then we can look at it again.
As far as input is concerned, your display() function currently (i.e. at the point where I'm writing this) does nothing, so you aren't going to be outputting sorted data, etc., and you are still outputting in the same loop as your input.
@lastchance; it's actually part of my assignment to use pointers and its working just fine for my first part (which is outputting the students records in 4 columns by ID, first name, last name, and GPA). If I delete my pointers, my code would not work the way I want it to.
I still recommend doing it first without pointers and then, if really necessary, modifying it to accommodate them.
We don't know what your assignment entails - many array operations can be done with a pointer-like syntax and that may be what was intended. That is different from what you are currently doing (having an array of pointers).
If you point to the memory address of a variable with a finite lifetime (e.g. the loop local variable studentInfo tmp;), then at the end of its lifetime (for tmp, the end of the loop) that variable, and hence the address you are pointing to, will cease to exist. The fact that it is working "fine" is simply that you output each student as soon as you read it and before the end of the loop.