Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //******************************************************************************
- // CSCI 1380.02 Spring 2016 Hw # 3
- // Hideki Garcia
- //
- // Simple program to practice data extraction from a file using the Standard Library FSTREAM
- //
- //******************************************************************************
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- const int MAX = 600;
- int average(int sum, int quantity);
- int main()
- {
- string name[MAX] = { " " };
- int grade[MAX] = { 0 };
- int quantity = 0;
- int x = 0;
- double avg = 0;
- int sum = 0;
- string n = " ";
- ifstream inFile;
- inFile.open("indata3.txt");
- if (!inFile.is_open())
- {
- cout << "Could not open file myfile.txt." << endl;
- return 1; // 1 indicates error
- }
- while(!inFile.eof())
- {
- inFile >> name[x];
- inFile >> grade[x];
- x++;
- }
- inFile.close();
- cout << "Enter quantity of grades to be processed (" << x << "): " << endl;
- cin >> quantity;
- for (x = 0; x < quantity; x++)
- {
- sum = sum + grade[x];
- }
- avg = average(sum, quantity);
- cout << "Average grade: " << avg << endl;
- for (x = 0; x<quantity; x++)
- {
- cout << "Name: " << name[x] << " Grade: " << grade[x] << " Comments: ";
- if (grade[x]<avg)
- {
- cout << " below average" << endl;
- }
- else if (grade[x] > avg) {
- cout << " above average" << endl;
- }
- else {
- cout << " average" << endl;
- }
- }
- system("pause");
- return 0;
- }
- int average(int sum, int quantity)
- {
- double avg = 0;
- avg = static_cast<double>(sum) / static_cast<double>(quantity);
- avg = avg + .5;
- return static_cast<int>(avg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement