#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string choice;
cout << "Want to add a new class? (y or n)";
getline(cin, choice);
while(choice == "y")
{
ofstream wgpafile;
string course, chours, grade;
wgpafile.open("/home/ted/gpafile.txt", ios::app);
cout << "What class are you adding (no spaces)? ";
getline(cin, course);
cout << "How many credit hours is it? ";
getline(cin, chours);
cout << "What grade did you recieve? ";
getline(cin, grade);
wgpafile << course << " " << chours << " " << grade << "\n";
cout << "Want to add another class? (y or n)";
getline(cin, choice);
if(choice == "n")
wgpafile.close();
}
double tcredit = 0.0;
double tgpa = 0.0;
double ogpa = 0.0;
string course;
ifstream rgpafile;
rgpafile.open("/home/ted/gpafile.txt");
while(!rgpafile.eof())
{
double credit, gpa;
rgpafile >> course;
rgpafile >> credit;
rgpafile >> gpa;
tcredit = tcredit + credit;
cout << "tcredit a " << tcredit << endl;
tgpa += (gpa * credit);
}
cout << tgpa << " " << tcredit << " ";
ogpa = tgpa / tcredit;
cout << ogpa;
return 0;
}
The first while loop just adds the classes to a file, which works fine, but the second part where I read the values from the file doesn't work right, for some reason it is adding the last line of the file twice and I can't figure out why. If someone could help me out that would be great and also, if there is a better way to do what I am trying to do please let me know, I am trying to learn, so if something could be improved plese let me know, thanks.
Edited by WingedPanther, 11 September 2009 - 01:23 PM.
add code tags (the # button)


Sign In
Create Account


Back to top









