mercredi 11 mars 2020

While loop ending too late and doesnt stop when needed to c++

I am struggling with this code, It is suppose to take an input like this 5 90 80 70 60 50 where 5 is the number of repetitions and then stop. Then it takes the average, total of each type of grades, the total amount of grades, and the average of all those grades. if it reaches a cin of nothing, its supposed to default to outside the while loop and everything be equal to zero. if it encounters a zero, inbetween text its supposed to go through the while loop then stop. can anyone help? also I cannot use a for loop as my teacher said I can't. also getline is off the table as well.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
int main() {
   int As, Bs, Cs, Ds, Fs, number_of_grades, count,
      number_of_sections, highest, lowest, grade, total_number_of_grades;
   float  total, average, average_all, total_all;
   bool stop;
   cout << fixed << showpoint << setprecision(2);
   As = 0;
   Bs = 0;
   Cs = 0;
   Ds = 0;
   Fs = 0;

   lowest = 0;
   highest = 0;
   count = 0;
   total = 0;
   total_all = 0;
   grade = 0;
   number_of_grades = 0;

   number_of_sections = 0;
   average = 0;
   average_all = 0;
   total_number_of_grades = 0;


   while (cin) {

      cin >> number_of_grades;

      total_number_of_grades = number_of_grades + total_number_of_grades;

      while (number_of_grades > count) {

         cin >> grade;


         total = grade + total;

         total_all = grade + total_all;

         average_all = total_all / total_number_of_grades;



         if (grade >= 90) {
            As++;

         }
         else if (grade >= 80) {
            Bs++;

         }
         else if (grade >= 70) {
            Cs++;

         }
         else if (grade >= 60) {
            Ds++;

         }
         else {
            Fs++;

         }
         if (count == 0) {
            highest = grade;
            lowest = grade;

         }
         else if (grade > highest) {
            highest = grade;

         }

         else if (lowest > grade) {
            lowest = grade;

         }
         count++;




      }

         number_of_sections++;
         cout << "Scores for section " << number_of_sections << endl;
         cout << "A's: " << As << endl;
         cout << "B's: " << Bs << endl;
         cout << "C's: " << Cs << endl;
         cout << "D's: " << Ds << endl;
         cout << "F's: " << Fs << endl;

         if (number_of_grades == 0) {
            average = 0;
         }
         else {
            average = total / number_of_grades;
         }

         cout << "Lowest Section Score: " << lowest << endl;
         cout << "Highest Section Score: " << highest << endl;
         cout << "Average Section Score: " << average << endl;
         cout << endl;
         cout << endl;
         average = 0;
         highest = 0;
         lowest = 0;
         total = 0;
         count = 0;
         number_of_grades = 0;
         As = 0;
         Bs = 0;
         Cs = 0;
         Ds = 0;
         Fs = 0;
      }






   }
   if (total_number_of_grades == 0) {
      average_all = 0;
   }
   else {
      average_all = total_all / total_number_of_grades;
   }
   cout << "Total number of sections: " << number_of_sections << endl;
   cout << "Total number of scores: " << total_number_of_grades << endl;
   cout << "Class Average: " << average_all << endl;
   average_all = 0;
   cout << endl;

   cout << "That's all the sections!! Normal Termination.";
}

Aucun commentaire:

Enregistrer un commentaire