Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- //global const
- //const int names = 10;
- //const int testScore = 5;
- const int CAP = 100;
- const int MAXCHAR = 101;
- //function prototype
- void openFile(ifstream &inFile);
- int readData(ifstream &inFile, string names[], int scores[]);
- void calculateAVG(int size, string names[], int scores[]);
- int main()
- {
- string names[CAP];
- int scores[CAP] = { 0 };
- int size = 0;
- ifstream inFile;
- //open
- openFile(inFile);
- //read
- size = readData(inFile, names, scores);
- //calc
- calculateAVG(size, names, scores);
- return 0;
- }
- void openFile(ifstream &inFile)
- {
- // char filename[MAXCHAR];
- // cout << "enter name";
- // cin.get(filename, MAXCHAR);
- // ifstream inFile;
- inFile.open("arrays.txt");
- if(!inFile)
- {
- cout << "The file could not be found." << endl;
- exit(0);
- }
- }
- int readData(ifstream& inFile, string names[], int scores[][5]) {
- int size = 0;
- while (inFile >> names[size] && !inFile.eof())
- {
- for (int x = 0; x < 5; x++)
- {
- inFile >> scores[size][x];
- }
- size++;
- }
- return size;
- }
- /*
- int readData(ifstream& inFile, string names[], int scores[])
- {
- int size = 0;
- while (!inFile.eof())
- {
- inFile >> names[size] >> scores[size];
- size++;
- }
- return size;
- }
- */
- void calculateAVG(int size, string names[], int scores[])
- {
- int total = 0;
- double percent = 0;
- for (int i = 0; i < size; i++)
- total += scores[i];
- for (int i = 0; i < size; i++)
- {
- percent = (static_cast<double>(scores[i]) / total) * 100;
- cout << fixed << showpoint << setprecision(2);
- cout << left << setw(15) << names[i] << setw(8) << scores[i]
- << setw(5) << percent << "%" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment