Welcome to the world of C++, some of the things were mentioned already in other commends but i will try to summarize and explain.
well lets start that in C++ it is recommend from avoid using C Style array as C++ offer much better solution which is std::vector which is part of STL which is very central and important part of C++.
Also i would like to advice that in general when you write software it is important to try to avoid hard-coded limitation, for example you declare for every student an variable but is more generic of you can declare in run-time how many student will be. also what if you want to extend you program? suppose you also want to print all the marks that was entered? then it is very easy to traverse data in the std::vector! (which is preferable then store the sum in a variable when loop on the fly).
Another thing is try to make your program stable , for example what if user accidentally wrote 12w instead of 127, you can use exception and check you input for such cases. (used std::stod to check the input correctness)
also it is good to know that there is no need to re-invent the wheel as C++ have very large verity of algorithms that suitable for many tasks. for example std::accumulate can add all the values in the vector from begin() to end(). (which are iterators).
Now some of the things in the code most probably is unfamiliar for you but don't be "scared" as you can cover them one by one, comment\comment parts you want to enable\disable and understand how they work, as well you can try to improve the example that i gave..
and last is pay to what compiler you have, because for example std::reduce is available from C++ 17 and would not compile in older C++ version.
Below is rewrite of your program in a more stable and generic
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
size_t numberOfStudents;
std::cout << "How many learners there is in Class : ";
std::cin >> numberOfStudents;
std::vector<double> grades;
grades.reserve(numberOfStudents); // read about reserve when you learn how vector work.
for(size_t studentIndex = 0; studentIndex < numberOfStudents; ++studentIndex)
{
double mark;
std::string input;
std::cout << "Input mark for learner [" << studentIndex << "] : ";
std::cin >> input;
try
{
mark = std::stod(input); // std::stod will throw exception if the input is wrong.
}
catch(const std::invalid_argument& ex)
{
std::cout << "Error: input is Incorrect=[" << input << "]." << std::endl;
return -1;
}
grades.push_back(mark);
}
// when possible is it good practice to use const
const double totalSum = std::accumulate(grades.begin(), grades.end(),0.0);
std::cout << "Average: " << totalSum / numberOfStudents << std::endl;
return 0;
}
So in summary.
- Avoid using std::namespace and use std:: as practice.
- Prefer using std::vector<> instead of C style array.
- Use exception and\or check you input for incorrect values, array boundary, divide by zero etc.
- Use STL and learn how to use STL algorithms when you feel need to write loop.( in this case instead of writing loop that will summarize doubles in a std::vector) .
- Think about write generic, easy to read and extensible software.
Hope this will help you! and Good Luck!