SHARE
TWEET

inputFile >> "NamesGrades.txt"; is where I'm having trouble

corneliaprim Apr 21st, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Headers
  3. #include <iostream>     // cout, cin
  4. #include <cstdlib>      // exit()
  5. #include <string>       // strings
  6. #include <fstream>      // file processing
  7. #include <iomanip>      // stream manipulation
  8. using namespace std;
  9.  
  10. // Global variables
  11. const int MAX_STUDENTS = 25;    // We will not process more than 25 students even if the file contains more
  12. const int MAX_GRADES = 5;       // Each student has exactly 5 grades
  13. const string FILENAME = "NamesGrades.txt";  // The name of the file that you will read
  14.  
  15. // Function declarations
  16. int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int MAX_STUDENTS);
  17. void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount);
  18. void displayMax(string students[], int grades[][MAX_GRADES], int studentCount);
  19. void displayMin(string students[], int grades[][MAX_GRADES], int studentCount);
  20. char getLetterGrade(double grade);
  21. int getLongestNameLength(string students[], int studentCount);
  22.  
  23. int main()
  24. {
  25.     // You will need some variables here
  26.  
  27.  
  28.     string students[MAX_STUDENTS];  // You need an array of strings for the student names
  29.  
  30.     int grades[MAX_STUDENTS][MAX_GRADES]; // You need a two dimensional array of ints for the grades of the students
  31.  
  32.     int studentCount=0; // You need one to keep up with the actual number of students
  33.  
  34.     int choice=0;   // You need a variable to hold the choice of the user for the menu; choice is a char b/c it recognizes the value as a char and not a number
  35.  
  36.     studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
  37.  
  38.     // Loop selections until user says to quit
  39.     do {
  40.         // present menu and get user's choice
  41.         cout << "Select 1 for the average." << endl;
  42.         cout << "Select 2 for the maximum." << endl;
  43.         cout << "Select 3 for the minimum." << endl;
  44.         cout << "Select 4 to quit the program" << endl;
  45.  
  46.         // Process the choice
  47.         cin >> choice;
  48.  
  49.             switch (choice)
  50.             {
  51.             case 1: displayAverages(students, grades, studentCount);
  52.                 break;
  53.  
  54.             case 2:displayMax(students, grades, studentCount);
  55.                 break;
  56.  
  57.             case 3:displayMin(students, grades, studentCount);
  58.                 break;
  59.  
  60.             case 4: //break closes the program
  61.                 break;
  62.  
  63.             default:
  64.                 cout << "Invalid choice. Try again." << endl;
  65.  
  66.             }//note that cases and switches are chars to find the location of the character
  67.  
  68.         cout << endl;
  69.     } while (choice != 4);
  70.  
  71.     cout << "You quit the program." << endl; //end message & program
  72.     system("pause");
  73.  
  74.     return 0;
  75. }
  76.  
  77. /***********************************************************
  78. loadStudentNameGrades opens and read fileName. It will read in two strings, concatenate them, and then save
  79. to the students array. It then reads five integers and save each to the grades array. The function will return
  80. the actual number of student/grade combinations read
  81. PARAM:  students is an array of strings that can hold up ot maxStudents values
  82.         grades is a two dimensional array for holding the grades of each student
  83.         fileName is the name of the file that will be opened and read
  84.         maxStudents is the maximum number of students that we will read from the file
  85. PRE:    students[] is large enough to contain up to maxStudents elements
  86.         grades[] is large enough ot contain up to maxStudents elements
  87. POST:   students[] contains the names of up to maxStudents
  88.         grades[][] contains the grades for up to maxStudents
  89.         The number of student/grade combinations actually read from the file is returned. This value can range
  90.         between 0 <= numStudents <= maxStudents
  91. NOTE:   students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student
  92. ************************************************************/
  93. int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int MAX_STUDENTS)
  94. {
  95.     ifstream inFile;
  96.     string firstName, lastName;
  97.     int numStudents=0;
  98.  
  99.     inFile.open("NamesGrades.txt");
  100.  
  101.     if (!inFile) //Creates a message if is does not load the file
  102.     {
  103.         throw "This is not working";
  104.     }
  105.     for (int row = 0; row < MAX_STUDENTS && (inFile >> firstName >> lastName); row++, numStudents++)
  106.     {
  107.         inputFile >> "NamesGrades.txt";
  108.     }
  109.  
  110. return numStudents; // for stub out purposes, change this in your code
  111. }
  112.  
  113. /***********************************************************
  114. displayAverages calculates the average of each student and displays the
  115. students name, average, and letter grade of the average in a table
  116. PARAM:  students[] is an array of strings that contains the names of studentCount students
  117.         grades[] is an array of integers that contains the grades of studentCount students
  118.         studentCount contains the value of the number of elements in the students[] and grades[] arrays
  119. PRE:    students[] and grades[] contain values for studentCount elements
  120. POST:   table of student names, averages, and letter grades is displayed
  121. ************************************************************/
  122. void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
  123. {
  124.     double total = 0;
  125.     double average = 0;
  126.     for (int row = 0; row < studentCount; row++)
  127.     {
  128.         for (int col = 0; col < MAX_GRADES; col++)
  129.         {
  130.             total += (grades[row][col]); //student names are located in the row
  131.         }
  132.         average = total / MAX_GRADES;
  133.         cout << "Here is the average for student " << setw(20) << students[row] << setw(20) << average << setw(20) << getLetterGrade(average) << endl;//cout for each student
  134.         total = 0;//reset total to 0 before it loops again}
  135.         average = 0; // to reset the average to 0 before it loops again
  136.     }
  137.  
  138. }
  139.  
  140. /***********************************************************
  141. displayMax calculates the maximum grade of each student and displays the
  142. students name, maximum grade, and letter grade of the maximum grade in a table
  143. PARAM:  students[] is an array of strings that contains the names of studentCount students
  144.         grades[] is an array of integers that contains the grades of studentCount students
  145.         studentCount contains the value of the number of elements in the students[] and grades[] arrays
  146. PRE:    students[] and grades[] contain values for studentCount elements
  147. POST:   table of student names, maximum grades, and letter grades is displayed
  148. ************************************************************/
  149. void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
  150. {
  151.     for (int row = 0; row < studentCount; row++)
  152.     {
  153.         int max = 0;
  154.         for (int col = 0; col < MAX_GRADES; col++)
  155.         {
  156.             if (grades[row][col] > max)
  157.                 max = grades[row][col];
  158.         }
  159.         cout << "The student with the highest grade is " << students[row] << setw(20) << "at"<< max << getLetterGrade(max) << endl;
  160.     }
  161. }
  162.  
  163. /***********************************************************
  164. displayMin calculates the minimum grade of each student and displays the
  165. students name, minimum grade, and letter grade of the minimum grade in a table
  166. PARAM:  students[] is an array of strings that contains the names of studentCount students
  167.         grades[] is an array of integers that contains the grades of studentCount students
  168.         studentCount contains the value of the number of elements in the students[] and grades[] arrays
  169. PRE:    students[] and grades[] contain values for studentCount elements
  170. POST:   table of student names, minimum grades, and letter grades is displayed
  171. ************************************************************/
  172. void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
  173. {
  174.     for (int row = 0; row < studentCount; row++)
  175.     {
  176.         int min = 0;
  177.         for (int col = 0; col < MAX_GRADES; col++)
  178.         {
  179.             if (grades[row][col] < min)
  180.                 min = grades[row][col];
  181.         }
  182.         cout << "The student with the lowest grade is " << students[row] << setw(20) <<"at" << min << getLetterGrade(min) << endl;
  183.     }
  184. }
  185.  
  186. /***********************************************************
  187. getLetterGrade converts a numerical grade to a letter grade
  188. PARAM:  grade is the numerical grade to convert. Expected range is 0 <= grade <= 100
  189. PRE:    grade contains a value in the correct range
  190. POST:   The corresponding letter grade of the numerical grade is returned
  191. ************************************************************/
  192. char getLetterGrade(double grade)//passes the numerical grade and converts it to a letter grade
  193.  
  194. {
  195.     char letterGrade = 0;
  196.     if (grade >= grade && grade <= 100)
  197.     {
  198.         letterGrade = 'A';
  199.     }
  200.     else if (grade >= 80 && grade <= 89)
  201.     {
  202.         letterGrade = 'B';
  203.     }
  204.     else if (grade >= 70 && grade <= 79)
  205.     {
  206.         letterGrade = 'C';
  207.     }
  208.     else if (grade >= 60 && grade <= 69)
  209.     {
  210.         letterGrade = 'D';
  211.     }
  212.     else
  213.     {
  214.         letterGrade = 'F';
  215.     }
  216.  
  217.     return letterGrade;
  218. }
  219.  
  220. /***********************************************************
  221. getLongestNameLength returns the length of the longest string from a list of strings
  222. PARAM:  students[] is an array of strings that contains the name of students
  223.         studentCount is the size of the students[] array
  224. PRE:    students[] contains studentCount names
  225. POST:   The length of the longest string in students[] is returned
  226. ************************************************************/
  227. int getLongestNameLength(string students[], int studentCount)
  228. {
  229.     int longestName = 0;
  230.  
  231.     for (int row = 0; row < studentCount; row++)//loops through the names and compares them to find the longest
  232.     {
  233.         if (students[row].length() > longestName) //I like length over size b/c it reminds me of my intro python stuff.
  234.         {
  235.             longestName = students[row].length();
  236.         }
  237.     }
  238.     return longestName;
  239. }1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top