-2

I have been working on a simple grade calculator, I am having trouble with the "labs" option spitting back a bad result, I am looking for a decimal percent, but I keep getting a very very large exponential number.

The specific part I am having trouble with is the calclabavg() function-- the for loop to be exact.

I am not asking for an exact solution, I just want to be pointed in the right direction so that I can solve the problem on my own.

Thanks so much in advance :)

float calclabavg(){//funciton that calculates user lab averages

    float x, vary, pointspos, sumpointspos, sumearned;

    cout << "How many labs in labs?" << endl;
    cin >> x;

    float totalpoints = 0;

    cout << "Do the points vary per lab? (Press 1 for yes, 0 for no)" << endl;
    cin >> vary;

    if (vary == 0){
        cout << "How many points were possible on each lab?" << endl;
        cin >> pointspos;
        sumpointspos = x * pointspos;
    }

    for (int i = 0; i < x; i++){
        float temp;
        cout << "What was your score on lab " << i + 1 << endl;
        cin >> temp;
        sumearned += temp;
        if (vary == 1){
            cout << "How mant points possible on lab " << i + 1 << endl;
            float pointsposvary;
            cin >> pointsposvary;
            sumpointspos += pointsposvary;
        }
        //pointspos = pointspos + temp;
    }

    return sumearned / sumpointspos;
}
aghast
  • 14,785
  • 3
  • 24
  • 56
  • 1
    "I just want to be pointed in the right direction". Ok, that's fine, [here's the link that will set you on the right direction](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). HTH. – Sam Varshavchik Feb 11 '17 at 23:51
  • Purchased a recommended book thx! – Connor Baele Feb 12 '17 at 00:27

1 Answers1

1

It seems as if you do not initialize sumearned. Statement sumearned = 0.0; at the beginning of calclabavg should bring you at least a step further. BTW: initialising also the other variables is good practice and makes code more stable.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58