Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ input data reading incorrectly
    text
    copied!<p>I am having a problem reading my input file. the program reads the input file perfectly and works great except for the fact that it reads the last persons name in the document twice. </p> <p>here is part of the program output showing hooper, grace name twice. thanks for looking</p> <pre><code>****Input file lab10data1.txt is open for reading. Number of student records read = 17 The following student(s) have the highest score Einstein, Albert List of All the Students in the class Student Name Test Score Grade ------------------------------------------ Fibonacci, Leonardo 63 D Huffman, David 79 C Augusta, Ada 91 A Goldbach, Christian 81 B Venn, John 100 A Church, Alonzo 72 C Einstein, Albert 105 F Fermat, Pierre 84 B Kruskal, Joseph 66 D Cantor, Georg 67 D Turing, Alan 85 B Chebysheva, PL 100 A DeMorgan, Augustus 79 C Karnaugh, Maurice 72 C Babbage, Charles 98 A Hooper, Grace 95 A Hooper, Grace 95 A // RIGHT HERE it reads grace twice </code></pre> <p>this is the code</p> <pre><code> #include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;string&gt; #include&lt;iomanip&gt;** using namespace std; struct StudentType { string studentName; int testScore;//Between 0 and 100 char grade; }; void PrintNameHeader(ostream&amp; out); bool OpenInputFile(ifstream&amp; inFile, string&amp; infilename ); //OPEN input file void Pause();// Pause void ReadStudentData(ifstream&amp; infile, StudentType student[], int&amp; );// Read student infp including first and last name and test score void AssignGrades(StudentType student[], int);//assign grades to each student int HighestScore(const StudentType student[], int );//Get the highest scores void PrintNamesWithHighestScore(const StudentType student[], int);//Print name/s with highest Scores void DisplayAllStudents(const StudentType student[], int);//Display all students void GetLowHighRangeValues(const StudentType student[], int, int&amp; , int&amp;);//for example a student types 50 100 , it will get all students within that range void DisplayStudentsInRange(const StudentType student[], int, int, int);// display students in that range void SortStudentsByName(StudentType student[], int);// sort students by name void SortStudentsByScore(StudentType student[], int);// sort students by test score highest to lowest void FormatNameScoreGrade(ostream&amp; out); const int NUM_STUDENTS = 20; int main() { ifstream infile; string inFilename; StudentType student[NUM_STUDENTS]; int numStudents = 0; PrintNameHeader(cout); if(!OpenInputFile(infile,inFilename)) return 1; ReadStudentData(infile, student, numStudents); infile.close(); AssignGrades(student, numStudents); PrintNamesWithHighestScore(student, numStudents); cout &lt;&lt;"List of All the Students in the class" &lt;&lt; endl; DisplayAllStudents(student, numStudents); Pause();//Pause int lownum, highnum; char answer; do { GetLowHighRangeValues(student, numStudents, lownum, highnum); DisplayStudentsInRange(student, numStudents, 70, 90); cout &lt;&lt;"Do you want to see another range of scores [y/n]?"; cin &gt;&gt; answer; } while( answer == 'y' || answer == 'Y'); SortStudentsByName(student, numStudents); Pause();//Pause SortStudentsByScore(student, numStudents); return 0; } //Function definitions void PrintNameHeader(ostream&amp; out) { //Display name header on screen } void FormatNameScoreGrade(ostream&amp; out) { out &lt;&lt; setw(10) &lt;&lt; " Student Name" &lt;&lt; setw(20) &lt;&lt;"Test Score" &lt;&lt; setw(10) &lt;&lt;"Grade" &lt;&lt; endl; out &lt;&lt;" ------------------------------------------" &lt;&lt; endl; } bool OpenInputFile(ifstream&amp; inFile, string&amp; infilename) { cout &lt;&lt; "Enter the name of the .txt file that you want to open for input.\n"; cout &lt;&lt; "Do not put spaces in the file name : "; cin &gt;&gt; infilename; cout &lt;&lt; endl; inFile.open(infilename.c_str()); if (inFile.fail()) { cout &lt;&lt; "Sorry, the input file " &lt;&lt; infilename &lt;&lt;" was not found"&lt;&lt; endl;\ return false; } cout &lt;&lt; "Input file " &lt;&lt; infilename &lt;&lt; " is open for reading.\n\n"; return true; } void Pause() { cout &lt;&lt; endl; cin.ignore(80, '\n'); cout&lt;&lt;"Please hit the enter key to continue...\n"; cin.get(); } void ReadStudentData(ifstream&amp; infile, StudentType student[], int&amp; numStudents) { string firstName, lastName; int testScore; int count = 0; while(infile) { infile &gt;&gt; firstName &gt;&gt; lastName &gt;&gt; testScore; student[count].studentName = lastName + ", " + firstName; student[count].testScore = testScore; count++; } numStudents = count; cout &lt;&lt; "Number of student records read = " &lt;&lt; numStudents &lt;&lt; endl; } void AssignGrades(StudentType student[], int numStudents) { int testscore; char grade; for(int i = 0; i &lt; numStudents; i++) { testscore = student[i].testScore; if(testscore &gt;= 90 &amp;&amp; testscore &lt;=100) grade = 'A'; else if(testscore &gt;= 80 &amp;&amp; testscore &lt;90) grade = 'B'; else if(testscore &gt;= 70 &amp;&amp; testscore &lt;80) grade = 'C'; else if(testscore &gt;= 60 &amp;&amp; testscore &lt;70) grade = 'D'; else grade = 'F'; student[i].grade = grade; } } int HighestScore(const StudentType student[], int numStudents) { int highest = student[0].testScore; for(int i = 1; i &lt; numStudents; i++) { if(highest &lt; student[i].testScore) highest = student[i].testScore; } return highest; } void PrintNamesWithHighestScore(const StudentType student[], int numStudents) { int highest = HighestScore(student, numStudents); cout &lt;&lt; endl &lt;&lt; "The following student(s) have the highest score" &lt;&lt; endl &lt;&lt; endl; for(int i = 0; i &lt; numStudents; i++) { if (student[i].testScore == highest) { cout &lt;&lt; " " &lt;&lt; student[i].studentName; } } cout &lt;&lt; endl; } void DisplayAllStudents(const StudentType student[], int numStudents) { cout &lt;&lt; endl; FormatNameScoreGrade(cout); for(int i = 0; i &lt; numStudents; i++) { cout &lt;&lt; setw(20) &lt;&lt; student[i].studentName &lt;&lt; setw(10) &lt;&lt; student[i].testScore &lt;&lt; setw(10) &lt;&lt; student[i].grade &lt;&lt; endl; } cout &lt;&lt; endl; } void GetLowHighRangeValues(const StudentType student[], int numStudents, int&amp; lowRange, int&amp; highRange) { int lowest = student[0].testScore; for(int i = 1; i &lt; numStudents; i++) { if(lowest &gt; student[i].testScore) lowest = student[i].testScore; } lowRange = lowest; highRange = HighestScore(student, numStudents); } void DisplayStudentsInRange(const StudentType student[], int numStudents, int lownum, int highnum) { cout &lt;&lt;"Enter two vales for the range of scores you want to look at: "; cin &gt;&gt; lownum &gt;&gt; highnum; cout &lt;&lt; endl &lt;&lt; "List of students with scores in the range " &lt;&lt; lownum &lt;&lt; " to " &lt;&lt; highnum &lt;&lt; endl &lt;&lt; endl; FormatNameScoreGrade(cout); for(int i = 0; i &lt; numStudents; i++) { if(student[i].testScore &gt;= lownum &amp;&amp; student[i].testScore &lt;= highnum) { cout &lt;&lt; setw(20) &lt;&lt; student[i].studentName &lt;&lt; setw(10) &lt;&lt; student[i].testScore &lt;&lt; setw(10) &lt;&lt; student[i].grade &lt;&lt; endl; } } cout &lt;&lt; endl; } void SortStudentsByName(StudentType student[], int numStudents) { StudentType temp; for(int i = 0;i &lt; numStudents - 1;i++) { for(int j = i + 1; j &lt; numStudents;j++) { if(student[i].studentName &gt; student[j].studentName) { temp = student[i]; student[i] = student[j]; student[j] = temp; } } } cout &lt;&lt; endl; cout &lt;&lt; "List of Students sorted by Name" &lt;&lt; endl; DisplayAllStudents(student, numStudents); } void SortStudentsByScore(StudentType student[], int numStudents) { StudentType temp; for(int i = 0;i &lt; numStudents - 1;i++) { for(int j = i + 1; j &lt; numStudents;j++) { if(student[i].testScore &lt; student[j].testScore) { temp = student[i]; student[i] = student[j]; student[j] = temp; } } } cout &lt;&lt;"List of Students sorted by Score" &lt;&lt; endl; DisplayAllStudents(student, numStudents); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload