sentinel controlled while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 17
Reputation: dal4488 is an unknown quantity at this point 
Solved Threads: 0
dal4488 dal4488 is offline Offline
Newbie Poster

sentinel controlled while loop

 
0
  #1
Mar 29th, 2005
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??

  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. const int sentinel = -999;
  11.  
  12. ifstream inFile;
  13. string grades;
  14. int num;
  15. int sum = 0;
  16. int count = 0;
  17.  
  18.  
  19. inFile.open("C:\\Ch5_GPAInput.txt");
  20. getline(inFile, grades);
  21.  
  22. cout << "Line 1: Enter the numbers ending with " << sentinel << endl;
  23. cin >> num;
  24. while (num != sentinel)
  25. {
  26. sum = sum + num;
  27. count++;
  28. cin >> num;
  29. }
  30.  
  31. cout << "Line 7: The sum of the " << count << " numbers is " << sum << endl;
  32.  
  33. if (count != 0)
  34. cout << "Line 9: The average is " << sum / count << endl;
  35. else
  36. cout << "Line 11: No input." << endl;
  37.  
  38. inFile.close();
  39. system("PAUSE");
  40. return 0;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: sentinel controlled while loop

 
0
  #2
Mar 31st, 2005
>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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC