I am to write a program in which the user is prompted 4 times to enter a student's name, # of current hours, # of total hours, # of current quality points, # of total quality points, and course names. The program is to calculate the overall GPA of the student and write all information to output file "outfile.dat"
I am having trouble in writing the information to the output file. When I run the program, the output file heading is shown but not any of the student information. Suggestions??
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
int main()
{
int i, j, coursenum, hours, total_hours, qp, total_qp;
float gpa;
char name[20], course[8], dummy;
ofstream report("outfile.dat"); // setup the output data file
// send data to output file called outfile.dat
report << " ET 212-XX Program Three, Fall 2011" << endl;
report << "-----------------------------------------------------" << endl;
report << endl;
report << endl;
report << "# Student Name Quality Points Hours Earned GPA Courses Fall 2011" << endl;
report << endl;
report << "==========================================================================" << endl;
// setup the for loop
for (i=1; i <= 4; i++)
{ // begin loop
cout << "Enter Student Name: "; // prompt user for a student name
cin.get(name,20);
cin.get(dummy); // read the extra new line into the dummy field
cout << endl;
j = strlen(name);
j = 20 - j;
cout << "Enter number of hours: "; // prompt user for current hours
cin >> hours;
cout << endl;
cout << "Enter number of cummulitive hours: "; // prompt user for cummulitive hours
cin >> total_hours;
cout << endl;
cout << "Enter number of quality points this semester: ";// prompt user for current quality points
cin >> qp;
cout << endl;
cout << "Enter number of total quality points: "; // prompt user for total quality points
cin >> total_qp;
cout << endl;
cout << "Enter # of courses enrolled for this semester: "; // acquire # of courses for this semester per student
cin >> coursenum;
cout << endl;
total_hours = total_hours + hours; // add semester hours to cummulitive hours
total_qp = total_qp + qp; // add cummulitive quality points to quality points
gpa = total_qp / total_hours; // caculate GPA
report.setf(ios::fixed, ios::floatfield); // use c++ commands to include decimal point in output
report.setf(ios::showpoint);
report.open("outfile.dat"); // output the data to the data file
report << left << name[i] << total_qp[name] << total_hours[name] << gpa << course;
for (j = 1; j <= coursenum; j++) // set up inner loop
{ // begin inner loop
cout << "Enter course name: ";
cin >> course;
cout << endl;
} // end inner loop
report << left << setw(50) << course[j] << endl;
report << "-------------------------------------------------------------------------" << endl;
cin.get(dummy);
} // end outter loop
report << endl;
report.close(); // close data file
return 0;
} // end program
You don't write anything to outfile.dat inside your inner loop. Once you exit the inner loop, you attempt to write a single character, course[j], to the file. At this point, j is set to (coursenum + 1), so it's anyone's guess as to what character that is.
What are you trying to achieve with:
1 2
j = strlen(name);
j = 20 - j;
You don't use j again until you set it to 1 at the start of the inner loop, so those two lines of code don't have any effect.