Problem with using nested loop with file I/O

Hey guys,


I have problem using the nested loop with file I/O. Every time I debug the program, the name of the student in the output file doubled. I used txt for the output and input for the data

This is the work description
________________________________________
Write a C++ program to calculate the course grade based on the following criteria:

• Lab exercises (10 labs): 20%
• Homework (8 sets): 10%
• Midterm: 30%
• Final Exam: 40%
Grade letter assignment:
90% ‐ 100% A
85% ‐ 90% A‐
80% ‐ 85% B+
75% ‐ 80% B
70% ‐ 75% B‐
65% ‐ 70% C+
60% ‐ 65% C
55% ‐ 60% C‐
50% ‐ 55% D+
45% ‐ 50% D
40% ‐ 45% D‐
0% ‐ 40% F

Program should read an input file to get lab grades, homework grades, midterm exam grade, and final exam grade. The program should calculate the final course grade based on the above criteria. The format of the data in the input file is as follows:
Firstname Lastname lab1 lab2 lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 hw1, hw2 hw3 hw4 hw5 hw6 hw7 hw8 midterm final
There are 30 students in the class and each line in the input file has the information for one student. All the information are separated by a SPACE character. The program should output a file with the following information: student name (firstname and lastname), final grade and final letter grade on each line. At the end of the file, average grade for lab experiments and average grade for the homework sets should be printed.


_________________________________________

This is my work
_________________________________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
int count;
int LabExercises;
string firstname, lastname;
int lab1;
int lab2;
int lab3;
int lab4;
int lab5;
int lab6;
int lab7;
int lab8;
int lab9;
int lab10;
int AverageLabGrade;
int Homework;
int homework1;
int homework2;
int homework3;
int homework4;
int homework5;
int homework6;
int homework7;
int homework8;
int AverageHomeworkGrade;
int Midterm;
int Final;
int Total;
ifstream intfile;
ofstream outfile;


intfile.open("inputfile.txt");
outfile.open("outfile.txt");
if (!intfile) // Opening failed
{
cout << "Unable to open" << endl;

return 1;
}


intfile >> firstname >> lastname >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> homework1 >> homework2 >> homework3 >> homework4 >> homework5 >> homework6 >> homework7 >> homework8 >> Midterm >> Final;
while (intfile)
{
count=0;
while (count < 20)
{

outfile << firstname << lastname << endl;


intfile >> firstname >> lastname >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> homework1 >> homework2 >> homework3 >> homework4 >> homework5 >> homework6 >> homework7 >> homework8 >> Midterm >> Final;
count++;


//Print Average Grade
AverageLabGrade=((lab1+lab2+lab3+lab4+lab5+lab6+lab7+lab8+lab9+lab10)/10)*0.2;

outfile << "Average Lab Grade is: " << AverageLabGrade << endl;

//Print Homework grade

AverageHomeworkGrade=((homework1+homework2+homework3+homework4+homework5+homework6+homework7+homework8)/8)*0.1;

outfile << "Average Homework Grade is: " << AverageHomeworkGrade << endl;

//Print Midterm grade

Midterm=Midterm*0.3;
outfile << "Midterm Grade is: " << Midterm << endl;

//Print Final grade

Final=Final*0.4;
outfile << "Final Exam Grade is: " << Final << endl;

//Print Total
Total=(AverageLabGrade+AverageHomeworkGrade+Midterm+Final);

outfile << "Final Course Grade is: " << Total << endl;

if (Total >= 90)
outfile << "You Got A " << endl;
else if (Total >= 85 && Total <= 90)
outfile << " You Got A- " << endl;
else if (Total >= 80 && Total <= 85)
outfile << " You Got B+" << endl;
else if (Total >= 75 && Total <= 80)
outfile << " You Got B " << endl;
else if (Total >= 70 && Total <= 75)
outfile << " You Got B-" << endl;
else if (Total >= 65 && Total <= 70)
outfile << " You Got C+ " << endl;
else if (Total >= 60 && Total <= 65)
outfile << " You Got C" << endl;
else if (Total >= 55 && Total <= 60)
outfile << " You Got C- " << endl;
else if (Total >= 50 && Total <= 55)
outfile << " You Got D+" << endl;
else if (Total >= 45 && Total <= 50)
outfile << " You Got D " << endl;
else if (Total >= 40 && Total <= 45)
outfile << " You Got D-" << endl;
else if (Total >= 0 && Total <= 40)
outfile << " You Got F" << endl;

}

intfile >> firstname >> lastname >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> homework1 >> homework2 >> homework3 >> homework4 >> homework5 >> homework6 >> homework7 >> homework8 >> Midterm >> Final;


}




return 0;
}
_____________________


If anyone know where is the error/s, please help


It looks like it's possible you're reading the file too many times, but because of the lack of formatting it's extremely hard to read your code. Try placing your code into code tags.

Also have you studied std::vector or arrays and structures? This program is really screaming for the use of a structure containing a couple of arrays to hold the data from the file. And some functions wouldn't be a bad idea either.

Topic archived. No new replies allowed.