Note that there are some explanatory texts on larger screens.

plurals
  1. POfstream infinite loop while reading file in c++
    primarykey
    data
    text
    <p>I'm in a CS101 class at my school using C++. I have a program that has to read a file in this format:</p> <pre><code>Ramirez, Manny 1572838992 a 4 b 5 x 4 a 7 c 3 c 4 * Kemp, Matt 3337474858 a 4 b 4 b 4 a 4 * </code></pre> <p>It is supposed to read the file, calculate the GPA of each student and output the student's name, ID, units, and GPA. I have been trying different things for a while now, and I've been going back and forth with infinite loops. I got help from a friend, who had me include some stuff. </p> <p>I have tried changing the condition of the loop inside the while (!eof) loop so many times I can't count them all. Every time I look up what to do online, the advice I get is just to not use while !eof because it has a propensity to cause infinite loops. Well, I know that now, but my professor wants us to do it that way. </p> <p>Here is my code:</p> <pre><code> #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;iomanip&gt; #include &lt;sstream&gt; using namespace std; int main() { char grade; int units=0, studentsGrade=0, studentUnits=0; int unitTotal=0; float gradeTotal=0, gpa = 0; string inputName, outputName, fullName; long ID; ifstream inputFile; ofstream outputFile; cout &lt;&lt; "Please enter the input filename: "; cin &gt;&gt; inputName; cout &lt;&lt; "Please enter the output filename: "; cin &gt;&gt; outputName; inputFile.open(inputName.c_str()); if (inputFile.fail()) cout &lt;&lt; "Bad input file name." &lt;&lt; endl; else { outputFile.open(outputName.c_str()); outputFile &lt;&lt; left &lt;&lt; setw(25) &lt;&lt; "Name" &lt;&lt; setw(15) &lt;&lt; "ID" &lt;&lt; setw(15) &lt;&lt; "Units" &lt;&lt; setw(15) &lt;&lt; "GPA" &lt;&lt; endl; outputFile &lt;&lt; "--------------------------------------------------------------------------------" &lt;&lt; endl; cout &lt;&lt; left &lt;&lt; setw(25) &lt;&lt; "Name" &lt;&lt; setw(15) &lt;&lt; "ID" &lt;&lt; setw(15) &lt;&lt; "Units" &lt;&lt; setw(15) &lt;&lt; "GPA" &lt;&lt; endl; cout &lt;&lt; "--------------------------------------------------------------------------------" &lt;&lt; endl; getline(inputFile, fullName); while (!inputFile.eof()) { gpa = 0; unitTotal = 0; gradeTotal = 0; inputFile &gt;&gt; ID; outputFile &lt;&lt; setw(25) &lt;&lt; fullName; outputFile &lt;&lt; setw(15) &lt;&lt; ID; cout &lt;&lt; setw(25) &lt;&lt; fullName &lt;&lt; setw(15) &lt;&lt; ID; string line; getline(inputFile,line); istringstream iss(line); while (!iss.eof()) { units = 0; iss &gt;&gt; grade &gt;&gt; units; if (grade == '*') break; if (units &gt; 0 &amp;&amp; units &lt;=5 &amp;&amp; (grade == 'a' || grade == 'A')) { gradeTotal += 4 * units; studentsGrade += 4 * units; unitTotal += units; studentUnits += units;} else if (units &gt; 0 &amp;&amp; units &lt;=5 &amp;&amp; (grade == 'b' || grade == 'B')) { gradeTotal += 3 * units; studentsGrade += 3 * units; unitTotal += units; studentUnits += units; } else if (units &gt; 0 &amp;&amp; units &lt;=5 &amp;&amp; (grade == 'c' || grade == 'C')) { gradeTotal += 2 * units; studentsGrade += 2 * units; unitTotal += units; studentUnits += units; } else if (units &gt; 0 &amp;&amp; units &lt;=5 &amp;&amp; (grade == 'd' || grade == 'D')) { gradeTotal += 1 * units; studentsGrade += 1 * units; unitTotal += units; studentUnits += units; } else if (units &gt; 0 &amp;&amp; units &lt;=5 &amp;&amp; (grade == 'f' || grade == 'F')) { unitTotal += units; studentUnits += units; } else if (grade == '*') { unitTotal += 0;} else { unitTotal += 0; } } gpa = (float)gradeTotal / unitTotal; outputFile &lt;&lt; fixed &lt;&lt; showpoint; outputFile &lt;&lt; setw(15) &lt;&lt; unitTotal &lt;&lt; setw(15) &lt;&lt; setprecision(2) &lt;&lt; gpa &lt;&lt; endl; cout &lt;&lt; fixed &lt;&lt; showpoint; cout &lt;&lt; setw(15) &lt;&lt; unitTotal &lt;&lt; setw(15) &lt;&lt; setprecision(2) &lt;&lt; gpa &lt;&lt; endl; getline(inputFile,fullName); } outputFile &lt;&lt; "The GPA for all students is " &lt;&lt; setprecision(2) &lt;&lt; (float)studentsGrade / studentUnits; cout &lt;&lt; "The GPA for all students is " &lt;&lt; setprecision(2) &lt;&lt; (float)studentsGrade / studentUnits &lt;&lt; endl; } inputFile.close(); outputFile.close(); cout &lt;&lt; endl; return 0; } </code></pre> <p>If anyone can explain to me why I keep getting an infinite loop, I would really appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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