0

I'm having quite a difficult time with this program. I'm trying to write a program that calculates the average of the GPAs for male and female students. Each line of my file has an f or an m followed by a GPA. I have to use a sentinel controlled while loop, but I'm having a heck of a time. I'm not really sure what to do??

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{

const int sentinel = -999;

ifstream inFile;
string grades;
int num;
int sum = 0;
int count = 0;


inFile.open("C:\\Ch5_GPAInput.txt");
getline(inFile, grades);

cout << "Line 1: Enter the numbers ending with " << sentinel << endl;
cin >> num;
while (num != sentinel)
{
      sum = sum + num;
      count++;
      cin >> num;
}

cout << "Line 7: The sum of the " << count << " numbers is " << sum << endl;

if (count != 0)
      cout << "Line 9: The average is " << sum / count << endl;
else
      cout << "Line 11: No input." << endl;

inFile.close();
system("PAUSE");
return 0;
2
Contributors
1
Reply
3
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

>I'm not really sure what to do?

First, read from the file, not cin. Next, if your file has an f or m preceding the GPA, you'll need to read this character. Then, depending on which it was, you'd adjust one of the two running sums.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.