Jump to content

GPA Calculator

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
tedmp0816

tedmp0816

    Newbie

  • Members
  • PipPip
  • 12 posts
I am kind of a beginner at C++, and I am trying to make a simple gpa calculator, here is what I have:
#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)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I haven't tested it, but try this:
    rgpafile.open("/home/ted/gpafile.txt");

    rgpafile >> course;
    while(!rgpafile.eof())
    {
        double credit, gpa;

        rgpafile >> credit;
        rgpafile >> gpa;

        tcredit = tcredit + credit;
        cout << "tcredit a " << tcredit << endl;
        tgpa += (gpa * credit);
        rgpafile >> course;
    }

What I believe is happening is that you read the last line, which means you have NOT hit the EOF mark. You then go through and "read" the last line a second time.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
tedmp0816

tedmp0816

    Newbie

  • Members
  • PipPip
  • 12 posts
That worked thanks a lot, and yeah that makes sense. Also if anyone knows a more efficient way to do this that be cool. Also, the way I am storing the gpas on my computer, is that how data is normally saved on a computer (in a text file), or is there some different way?

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Normally I would use a text file, with the values in a standard order on each line.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz