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;
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.