| | |
sentinel controlled while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 17
Reputation:
Solved Threads: 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??
C++ Syntax (Toggle Plain Text)
#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;
>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.
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
![]() |
Similar Threads
- Programming Problem (C++)
- So Lost writing this program... (C++)
- Help please (C++)
- Need help for simple program, don't have a clue about C (C)
- C++ help (C++)
- Newby girl needing desparate help (Java)
- i need ur help (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ help for a newbie
- Next Thread: Change Font Size... c++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






