c++ - Last element added to an array is being added twice, unable to figure out why -
realization: wow spent time staring @ code noticed i'd been missing along. initialize = size means @ start array looking @ unfilled spot understand why puts repeat. attempt fix it. feel free berate me.
i'm looking through array backwards because i'm using insertion sort algorithm work backwards last known element alphabetize data. no matter final line in data file (from i'm reading) repeats twice. here array contents printed demonstrate repeat:
list of names sorted: 100 bill gates 100 bill gates 65 duck donald 60 frog freddie 71 ghost casper 85 mouse abby 73 mouse mickey 95 mouse minnie
notice bill gates listed twice. problem appears way i'm looping. if modify lower bound in loop 1 instead of 0 shit goes haywire. here function in question, don't believe of code outside relevant did not include it:
bool sortinput(ifstream &infile, studenttype students[], int &size) { studenttype temp; //empty condition if(size == 0) { infile >> temp.last >> temp.first >> temp.grade; strcpy(students[0].last, temp.last); strcpy(students[0].first, temp.first); students[0].grade = temp.grade; size++; } while(infile) { infile >> temp.last >> temp.first >> temp.grade; if(temp.grade >= lowgrade && temp.grade <= maxgrade) { for(int = size; > 0; i--) { if(strcmp(temp.last, students[i-1].last) < 0) { students[i] = students[i-1]; students[i-1] = temp; } else if(strcmp(temp.last, students[i-1].last) == 0 && strcmp(temp.first, students[i-1].first) < 0) { students[i] = students[i-1]; students[i-1] = temp; } else { students[i] = temp; break; } } size++; //tester loop print contents every step of way for(int = 0; < size; i++) { cout << "test: " << students[i].last << " " << students[i].first << " " << students[i].grade << endl; } cout << "done" << endl; } //end loop } //end while loop return true; }
however if full code necessary further context here is:
// ---------------------------------------------------------------------------- // write meaningful doxygen comments , assumptions #include <string.h> #include <iostream> #include <iomanip> #include <fstream> using namespace std; int const maxsize = 100; // maximum number of records in total int const maxlength = 31; // maximum string length int const maxgrade = 100; // highest possible grade int const lowgrade = 0; // lowest possible grade int const group = 10; // group amount int const histogramsize = (maxgrade-lowgrade)/group + 1; // grouped group struct studenttype { // information of 1 student int grade; // grade of student char last[maxlength]; // last name (maxlength-1 @ most) char first[maxlength]; // first name (maxlength-1 @ most) }; // prototypes go here bool sortinput(ifstream &, studenttype [], int &); void displaylist(studenttype [], int); void sethistogram(int [], studenttype [], int); void displayhistogram(int []); int findaverage(studenttype [], int); //------------------------------- main ---------------------------------------- int main() { studenttype students[maxsize]; // list of maxsize number of students int size = 0; // total number of students int histogram[histogramsize]; // grades grouped group int average = 0; // average exam score, truncated // creates file object , opens data file ifstream infile("data1.txt"); if (!infile) { cout << "file not opened." << endl; return 1; } // read , sort input last first name bool successfulread = sortinput(infile, students, size); // display list, histogram, , class average if (successfulread) { displaylist(students, size); sethistogram(histogram, students, size); displayhistogram(histogram); average = findaverage(students, size); cout << "average grade: " << average << endl << endl; } return 0; } bool sortinput(ifstream &infile, studenttype students[], int &size) { studenttype temp; //empty condition if(size == 0) { infile >> temp.last >> temp.first >> temp.grade; strcpy(students[0].last, temp.last); strcpy(students[0].first, temp.first); students[0].grade = temp.grade; size++; } while(infile) { infile >> temp.last >> temp.first >> temp.grade; if(temp.grade >= lowgrade && temp.grade <= maxgrade) { for(int = size; > 0; i--) { if(strcmp(temp.last, students[i-1].last) < 0) { students[i] = students[i-1]; students[i-1] = temp; } else if(strcmp(temp.last, students[i-1].last) == 0 && strcmp(temp.first, students[i-1].first) < 0) { students[i] = students[i-1]; students[i-1] = temp; } else { students[i] = temp; break; } } size++; for(int = 0; < size; i++) { cout << "test: " << students[i].last << " " << students[i].first << " " << students[i].grade << endl; } cout << "done" << endl; } //end loop } //end while loop return true; } void displaylist(studenttype students[], int size) { cout << "list of names sorted:" << endl; for(int = 0; < size; i++) { cout << " " << students[i].grade << " " << students[i].last << " " << students[i].first << endl; } cout << endl; } void sethistogram(int histogram[], studenttype students[], int size) { int groupindex; for(int = 0; < size; i++) { groupindex = (students[i].grade - lowgrade) / group; histogram[groupindex]++; } } void displayhistogram(int histogram[]) { cout << "histogram of grades: " << endl; int bottombin = lowgrade; int binwidth = (maxgrade - lowgrade) / group - 1; int topbin = bottombin + binwidth; for(int = 0; < histogramsize; i++) { cout << bottombin << "--> " << topbin << ": "; for(int j = 0; j < histogram[i]; j++) { cout << "*"; } cout << endl; bottombin += binwidth + 1; topbin = min(topbin + binwidth + 1, maxgrade); } } int findaverage(studenttype students[], int size) { int total = 0; for(int = 0; < size; i++) { total += students[i].grade; } return total / size; } // ---------------------------------------------------------------------------- // functions meaningful doxygen comments , assumptions go here
you can combine stream extraction , testing stream state changing:
while(infile) { infile >> temp.last >> temp.first >> temp.grade; ... }
to
while(infile >> temp.last >> temp.first >> temp.grade) { ... }
this read stream , fail (return false) if read fails reason, including eof.
note: read this related question more explanation.
Comments
Post a Comment