Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Array subscript won't increment in switch statement properly

$
0
0
You will notice that I have different statements in each case in my switch statement. This is because I have been trying different methods to get my array to increment properly. I essentially want to increment the countGrades[0] subscript by 1 for every time 'A' is entered. Same goes for subscript [1] [2] [3] [4] for letter grades B C D E and F. For whatever reason I keep getting a negative number in output. Something along the lines of -80000000. We must use an array to store the number of grades and an array to store EACH individual grade as well. Thanks in advance!

void GradeBook::inputGrades()
{
   int grade; // grade entered by user
   int x=0; // counter

   cout << "Enter the letter grades." << endl
      << "Enter the EOF character to end input." << endl;

   // loop until user types end-of-file key sequence
   
   while ( ( grade = cin.get() ) != EOF && x < 99 )
   {
      // determine which grade was input
      switch ( grade ) // switch statement nested in while
      {
         case 'A': // grade was uppercase A
         case 'a': // or lowercase a
            // increment aCount
			countGrades[0]++; // countGrades[] stores the number of A's B's C's etc. letterGrades[] is the actual grade 1-100
			x++;
            break; // exit switch

         case 'B': // grade was uppercase B
         case 'b': // or lowercase b
            countGrades[1]++; // increment bCount
			letterGrades[x];
			x++;
            break; // exit switch
            
         case 'C': // grade was uppercase C
         case 'c': // or lowercase c
            countGrades[2]++; // increment cCount
			letterGrades[x];
			x++;
            break; // exit switch
            
         case 'D': // grade was uppercase D
         case 'd': // or lowercase d
            countGrades[3]++; // increment dCount
			letterGrades[x];
			x++;
            break; // exit switch
            
         case 'F': // grade was uppercase F
         case 'f': // or lowercase f
            countGrades[4]++; // increment fCount
			letterGrades[x];
			x++;
            break; // exit switch
            
         case '\n': // ignore newlines,
         case '\t': // tabs,
         case ' ': // and spaces in input
            break; // exit switch
            
         default: // catch all other characters
            cout << "\n\t******Incorrect letter grade entered.******"
               << " \n\tEnter a new grade.\n";
            break; // optional; will exit switch anyway
      } // end switch
   } // end while
} // end function inputGrades

// display a report based on the grades entered by user
void GradeBook::displayGradeReport()
{
   // display summary of results
   cout << "\n\nNumber of students who received each letter grade:" 
      << "\nA: " << countGrades[0] // display number of A grades
      << "\nB: " << countGrades[1] // display number of B grades
      << "\nC: " << countGrades[2] // display number of C grades 
      << "\nD: " << countGrades[3] // display number of D grades
      << "\nF: " << countGrades[4] // display number of F grades
      << endl;

   // calculate total grades
   int gradeCount = countGrades[0] + countGrades[1] + countGrades[2] + countGrades[3] + countGrades[4];

   // display class average
   // if user entered at least one grade
   if ( gradeCount != 0 )
   {
      // calculate total grades
      int gradeTotal = 4 * countGrades[0] + 3 * countGrades[1] + 2 * countGrades[2] + 1 * countGrades[3];

      // set floating-point number format
      cout << fixed << setprecision( 1 );

      // compute and display class GPA with 1 digit of precision
      cout << "\nThe class average is: " 
         << static_cast< double > ( gradeTotal ) / gradeCount


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>