Guest User

Untitled

a guest
Jun 3rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. //global const
  8. //const int names = 10;
  9. //const int testScore = 5;
  10. const int CAP = 100;
  11. const int MAXCHAR = 101;
  12. //function prototype
  13. void openFile(ifstream &inFile);
  14. int readData(ifstream &inFile, string names[], int scores[]);
  15. void calculateAVG(int size, string names[], int scores[]);
  16.  
  17. int main()
  18. {
  19.     string names[CAP];
  20.     int scores[CAP] = { 0 };
  21.     int size = 0;
  22.     ifstream inFile;
  23.     //open
  24.     openFile(inFile);
  25.     //read
  26.     size = readData(inFile, names, scores);
  27.     //calc
  28.     calculateAVG(size, names, scores);
  29.     return 0;
  30. }
  31.  
  32. void openFile(ifstream &inFile)
  33. {
  34. //  char filename[MAXCHAR];
  35. //  cout << "enter name";
  36. //  cin.get(filename, MAXCHAR);
  37. //  ifstream inFile;
  38.     inFile.open("arrays.txt");
  39.     if(!inFile)
  40.     {
  41.         cout << "The file could not be found." << endl;
  42.         exit(0);
  43.     }
  44. }
  45. int readData(ifstream& inFile, string names[], int scores[][5]) {
  46.     int size = 0;
  47.     while (inFile >> names[size] && !inFile.eof())
  48.     {
  49.         for (int x = 0; x < 5; x++)
  50.         {
  51.             inFile >> scores[size][x];
  52.         }
  53.         size++;
  54.     }
  55.     return size;
  56. }
  57. /*
  58. int readData(ifstream& inFile, string names[], int scores[])
  59. {
  60.     int size = 0;
  61.     while (!inFile.eof())
  62.     {
  63.         inFile >> names[size] >> scores[size];
  64.         size++;
  65.     }
  66.     return size;
  67. }
  68. */
  69.  
  70. void calculateAVG(int size, string names[], int scores[])
  71. {
  72.     int total = 0;
  73.     double percent = 0;
  74.     for (int i = 0; i < size; i++)
  75.         total += scores[i];
  76.     for (int i = 0; i < size; i++)
  77.     {
  78.         percent = (static_cast<double>(scores[i]) / total) * 100;
  79.         cout << fixed << showpoint << setprecision(2);
  80.         cout << left << setw(15) << names[i] << setw(8) << scores[i]
  81.             << setw(5) << percent << "%" << endl;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment