Search code examples
c++loopsnested-loops

How can I find the number of scores entered by a user are greater than 80?


I have been trying to code a program that takes input from the user for scores and then calculated the average with an input validation. The only thing I am unable to figure out is how to tell the number of scores entered which are greater than 80. Also I have to do this without using arrays.

Here's what I currently have but is not working and starts the counter from 5 instead of 1 and then incrementing it as the scores greater than 80 are entered.

int main()
{

    int score, sum=0, greater=0;
    
    for(int i=1; i<=5; i++)
    {
    
    
        cout<<"Enter the score: ";  //take user input for scores
        cin>>score;
        
        if(score>80)
        {
            for (int i=1; i<=5; i++){
                greater= greater+1;
                    
            }
            
            cout<<"There are "<<greater<<" number more than 80";
        
        }
        
        
        while (! (score >=0 && score <= 100 ))  //input validation
        {
            cout << "Invalid Input. Enter the score between the range 0 - 100" << endl;
            cout << "Enter the score: ";
            cin >> score;
      
        }
        
            sum = sum + score;      
        
    }
    
    float avg;
    
    avg = sum/5.0;  //calculating the average
    
    cout<<"Average of scores: "<<avg<<endl;

Can anybody help me with this? It would be much appreciated. Thanks!

I tried the above listed code and also tried to tweak it but it still shows the count as multiple of 5.


Solution

  • You mixed up the sequence of statements in your code.

    Strong hint: If you write comments, then you will avoid such problems.

    Please see below your corrected code

    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main() {
    
        int score, sum = 0, greater = 0;
        // Get 5 values from user
        for (int i = 0; i < 5; i++)
        {
            // We ant to validate the input and use the below to indicate the result
            bool validValue = false;
    
            // Now read values, until we get a valid one
            do {
    
                // We initially assume that the value is good
                validValue = true;
    
                // Get user input
                cout << "\n\nEnter a score. Valid values are 0...100: ";  //take user input for scores
                cin >> score;
    
                // Check, if the user entered some ivalid data like "abc"
                if (!cin) {
                    validValue = false;
                    // Reset error flag from cin
                    cin.clear();
                    // Discard the rest of invalid characters taht are still in the input stream
                    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // skip bad input
                }
                // Check for valid range
                else if (score < 0 or score > 100)
    
                    // Problem. Indicate
                    validValue = false;
    
                if (not validValue)
                    std::cout << "\nError: Invalid value given. Please try again\n";
                // contiinue the loo, until we get a valid value
            } while (not validValue);
    
            // Ok, now we have a valid value
    
            // Check, if the score meets our condition
            if (score > 80) 
                
                // Then counte up
                greater = greater + 1;
            
    
            // Calculate the overall sum, so that we can evaluate the average later
            sum = sum + score;
        }
        // So, now we have read 5 values
    
        // Calculate the avarage of all scores
        double avg;
        avg = sum / 5.0;  //calculating the average
    
        // And now show the result on the screen
        cout << "There are " << greater << " number more than 80\n";
        cout << "Average of scores: " << avg << '\n';
    }