Hi, I am having issues with my code that reads data from an input file, and outputs it to an output file in a nice ordered manner. It reads how many students (3-5)and for each student it is supposed to read a Student's personal info, and 5 grades for 3 different courses and calculated the final grades for each course. The problem is that my loop is:
A. Running too many times for what it should be doing
B. Only reading the first students data for each loop, and only the first set of grades
My code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
//Declaring the variables.
int studentNumber;
string name;
int age;
string address;
int years;
string tel;
string social;
string courseNum; //For storing the course number.
double test1; //For storing the grade for test 1.
double test2; //For storing the grade for test 2.
double test3; //For storing the grade for test 3.
double test4; //For storing the grade for test 4.
double exam; //For storing grade for the final exam.
double finalGrade; //For storing the calculated final grade for the class.
char grade; //For storing the calculated letter grade for the class.
const double test1Percentage = .10;
const double test23Percentage = .15;
const double test4Percentage = .20;
const double examPercentage = .40;
cout << "Please enter the number of students: ";
cin >> studentNumber;
ifstream fin;
ofstream fout;
fin.open("Project3_A04371627_Input.txt");
fout.open("Project3_A04371627_Output.txt");
//Informing the user
cout << "Reading the data files" << endl;
for (int a = 1; a <= studentNumber; a++)
{
getline(fin,name); //Reading the name
fin >> age; //Reading the age
fin.ignore (); //Ignore between fin and getline
getline(fin,address); //Reading the address
fin >> years; //Reading the years
fin.ignore (); //Ignore between fin and getline
getline(fin,tel); //Reading the telephone number
getline(fin,social); //Reading the social number
fout << "\t\t\t Student Grade Sheet" << endl;
fout << setw(35) << right << "Name of Student: ";
fout << setw(5) << left << name << endl;
fout << setw(35) << right << "Age: ";
fout << setw(5) << left << age << endl;
fout << setw(35) << right << "Address: ";
fout << setw(5) << left << address << endl;
fout << setw(35) << right << "Number of years at Texas State: ";
fout << setw(5) << left << years << endl;
fout << setw(35) << right << "Telephone Number: ";
fout << setw(5) << left << tel << endl;
fout << setw(35) << right << "Student Soc. Security #: ";
fout << setw(5) << left << social << endl;
for (int b = 1; b <=3; b++)
{
getline(fin,courseNum);
fout << setw(35) << right << "Course Number: ";
fout << setw(5) << left << courseNum << endl;
for (int c = 1; c <=5; c++)
{
fin >> test1; //Reading the grade for Test 1
fin >> test2; //Reading the grade for Test 2
fin >> test3; //Reading the grade for Test 3
fin >> test4; //Reading the grade for Test 4
fin >> exam; //Reading the grade for the Exam
//Calculating the value of the final grade.
finalGrade = (test1 * test1Percentage) + (test2 * test23Percentage) + (test3 * test23Percentage) + (test4 * test4Percentage) +(exam * examPercentage);
fout << setprecision(1) << showpoint << fixed << endl; //Formating test scores to have
//one digit after the decimal
fout << setw(35) << right << "Test #1: ";
fout << setw(5) << left << test1 << endl;
fout << setw(35) << right << "Test #2: ";
fout << setw(5) << left << test2 << endl;
fout << setw(35) << right << "Test #3: ";
fout << setw(5) << left << test3 << endl;
fout << setw(35) << right << "Test #4: ";
fout << setw(5) << left << test4 << endl;
fout << setw(35) << right << "Final Exam: ";
fout << setw(5) << left << exam << endl;
fout << setw(35) << right << "Numerical Grade in " << courseNum << ": ";
fout << setw(5) << left << finalGrade << endl;
//Determining letter grade
if(finalGrade < 60)
{
grade = 'F';
fout << setw(35) << right << "Letter Grade in " << courseNum << ": " << setw(5) << left << grade << endl;
}
else if (finalGrade < 70)
{
grade = 'D';
fout << setw(35) << right << "Letter Grade in " << courseNum << ": " << setw(5) << left << grade << endl;
}
else if (finalGrade < 80)
{
grade = 'C';
fout << setw(35) << right << "Letter Grade in " << courseNum << ": " << setw(5) << left << grade << endl;
}
else if (finalGrade < 90)
{
grade = 'B';
fout << setw(35) << right << "Letter Grade in " << courseNum << ": " << setw(5) << left << grade << endl;
}
else if (finalGrade <= 100)
{
grade = 'A';
fout << setw(35) << right << "Letter Grade in " << courseNum << ": " << setw(5) << left << grade << endl;
}
else
fout << "Invalid Grade!" << endl;
}
}
}
//Closing input and output files
fin.close();
fout.close();
//Informing the user
cout << "Final grade has been calculated.";
return 0;
}
|
My input file looks like this:
John Doe
24
1234 Right drive, Yellowtown, TX 73452
1
(281) 234 9876
123-45-6789
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
82.1
83.5
98.2
87.4
92.1
CS3002
95.4
92.1
87.4
78.6
77.5
With 3 more students and their course data just below the last grade above
and my output file looks like this:
Student Grade Sheet
Name of Student: John Doe
Age: 24
Address: 1234 Right drive, Yellowtown, TX 73452
Number of years at Texas State: 1
Telephone Number: (281) 234 9876
Student Soc. Security #: 123-45-6789
Course Number: CS1428
Test #1: 80.9
Test #2: 90.2
Test #3: 98.5
Test #4: 89.8
Final Exam: 97.7
Numerical Grade in CS1428: 93.4
Letter Grade in CS1428: A
Test #1: 0.0
Test #2: 90.2
Test #3: 98.5
Test #4: 89.8
Final Exam: 97.7
Numerical Grade in CS1428: 85.3
Letter Grade in CS1428: B
And it just loops the same data in the second paragraph above 15 times before repeating the SAME student data as the first student and all the same grade another 15 times twice.
Any help with what i did wrong would be mucho appreciated