0

I have to write a program that asks the user for a grade between 0 - 100 infinitely, once the user inputs the number "-1" it takes all of that data and gives the highest grade number along with the average.

I'm having trouble getting it to repeat the question and I'm puzzled as to how to store and calculate all of that data when it's completely random.

This is what I've come up with so far

#include <iostream>
using namespace std;

int main(){
   int num;

   cout<<"To get final and highest score, enter -1."<<endl;

   cout<<"Enter the score for student 1:  ";
   cin>>num;
   while (num < 0 || num > 100){
      cout<<"Wrong. You must enter a number between 0 to 100.\n";
      cin>>num;
   } 
   if (true){
      cout<<"Enter the grade for student 2: ";
      cin>>num;
   }

   return 0;
}
user2802841
  • 903
  • 7
  • 13
user3371154
  • 17
  • 1
  • 6
  • You don't need to store "all that data" - just the highest value... And you can compute the sum and number of entries to keep track of the average. That's cleaner than keeping all the numbers. And when the number is < 0 you must break from your loop, not say "wrong". That way you never get out... – Floris Mar 02 '14 at 17:09
  • `if (true){` is obviously redundant! – πάντα ῥεῖ Mar 02 '14 at 17:10
  • How would I compute the sum and numbers of entries? – user3371154 Mar 02 '14 at 17:14

3 Answers3

0

Some pseudocode:

main(){
  nMax;
  avg;
  end = false;
  num;
  nNums = 0;

  cout << "to exit enter -1";

  while(!end){
    do{
    cout << "Enter number";
    cin >> num;
    }while(num < -2 || num > 100);
    if ( num < 0){
      end = true;
    }else{
      avg = ((avg * nNums) + num) / ++nNums;
      nMax = max(nMax, num);
    }
  }
  cout << avg << ", " << nMax;
}
Javier Mr
  • 2,130
  • 4
  • 31
  • 39
0

Here you are:

#include <iostream>

int main(){
   int num;
   int sum=0, count=0;
   int maxGrade = -1;
   std::cout<<"To get final and highest score, enter -1."<<std::endl;

   while(true) {
     std::cout<<"Enter the score for student "<<count+1<<":"<<std::endl;
     std::cin>>num;
     if(num == -1) break;                // <<<< test specifically for break condition
     if(num < 0 || num > 100) {          // <<<< test for any other invalid input
      std::cout<<"Invalid grade. You must enter a number between 0 and 100.\n";
     }
     else {
       count++;                          // <<<< track number of valid inputs
       sum += num;                       // <<<< track total grade
       if(num>maxGrade) maxGrade = num;  // <<<< track highest grade so far
     }
   }
  std::cout<<"Number of students: "<<count<<std::endl;
  std::cout<<"Average is "<< sum * 1.0 / count<<std::endl;
  std::cout<<"Highest grade is "<<maxGrade<<std::endl;

return 0;
}

Sample output:

To get final and highest score, enter -1.
Enter the score for student 1:
78
Enter the score for student 2:
55
Enter the score for student 3:
98
Enter the score for student 4:
-2
Invalid grade. You must enter a number between 0 to 100.
Enter the score for student 4:
88
Enter the score for student 5:
-1
Number of students: 4
Average is 79.75
Highest grade is 98
Floris
  • 45,857
  • 6
  • 70
  • 122
  • Most of your std::endl should be replaced by '\n', you are flushing the stream without any need. – Klaim Mar 02 '14 at 17:27
  • @Klaim: I think that "should be replaced" is a bit strong in this context. I don't believe that a interactive program like this is terribly performance-critical. "Could be replaced" might be a better term to use. I think it's a matter of preference. See discussion [here](http://stackoverflow.com/a/8311177/1967396) for example. That does actually point out that the `cin` will flush the output buffer - so indeed you don't need to do it explicitly. – Floris Mar 02 '14 at 17:37
0

To calculate the highest grade and the average you need not to store all entered values. All what you need is to claculate the sum of entered numbers its quantity and the largest grade. The program could look the following way

#include <iostream>
using namespace std;

int main()
{
   bool empty = true;
   int sum = 0;
   int cnt = 0;
   int max;

   cout << "To get final and highest score, enter -1." << endl;

   while ( true )
   {
      cout << "Enter the score for student " << n + 1 << ":  ";

      int num = -1;

      cin >> num;

      if ( num == -1 ) break;

      if ( num < 0 || num > 100 )
      {
         cout << "Wrong. You must enter a number between 0 to 100.\n";
      }
      else
      {
         if ( empty || max < num ) max = num;

         empty = false;
         sum += num;
         ++n;
      } 
   }

   if ( !empty )
   {
      cout << "The maximum  score is " << max << endl;
      cout << "The average score is " << sum / n << endl; 
//    cout <<  "The average score is " << ( sum + 0.0 ) / n << andl; //that to get a float number    
   }

   return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335