0

So i wanted to make a simple average taker in c++. But I also want to include a guard to check if the input is a number(which explains the purpose of (!cin). I tried to do it but i cant seem to restart the code and it just continues unto the loop without reseting the input. This is the simple code for getting average of multiple numbers given the limit.

Here's the code to simply getting average of multiple numbers.

#include <iostream>

using namespace std;

int main(){

    
        int numval, v;
        float val[100],  average, sum = 0.0;
        cout << "Number of Values to Calculate: ";
        cin >> numval;
        
        while (numval > 100 || numval <= 1){
            cout << "The range is only from 2 - 100.";
            cout << "Enter another input.\n";
            cin >> numval;
    }
        for(v = 0; v < numval; ++v){
            cout << v + 1 <<  ". Enter the values: ";
            cin >> val[v];
            sum += val[v];
        }
        average = sum / numval;
        cout << "Average of " << numval << " given values is " << average;
            
    }
Cerph
  • 1
  • 1

0 Answers0