-4

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.

Tehreem
  • 17
  • 1
  • If you use a loop with 5 iterations, that increment a number by one each, you'll go from 0 to 5, not from 0 to 1. Also the input validation should probably happen before using the input. – fabian Nov 05 '22 at 12:45
  • 1
    I think you need to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. A simple [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) is a good start. Actually using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line will definitely show you a few problems. – Some programmer dude Nov 05 '22 at 12:46

2 Answers2

0

From what I understand, thenfor loop that is present inside the if statementnisnnot needed. Just keep the increment logic for greater variable and the inner for loop. Also move the print atatement inside the if statement after the outside for loop. This would give you the number of scores entered by user greater than 80

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 01:56
0

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';
}
A M
  • 14,694
  • 5
  • 19
  • 44