I am trying to make a simple program that will tell me the mean of a set of numbers. The problem I have is that the user must input the exact amount of numbers as I have variables. How do I make a loop system that will take a new input each time it runs and attach a new variable name to that input so the user can input as many or as little variables as they want?
#include <vector>
#include <iostream>
int main ()
{
double* values;
int n;
double total = 0;
// n is the total number of values user is providing
std::cin >> n;
values = newdouble [n];
for( int i = 0 ; i < n ; i ++ ) {
std::cin >> values[i];
total += values[i];
}
std::cout << total / n;
return 0;
}
You can use this with a notepad that has a list of all values
input.txt
10
1 4 -3 6.7 3
7 56 -5 78 23
If you are using Windows, you can open cmd and provide input from a file like this
example.exe < input.txt
If you must receive input from a user directly without using file, you can simply ask user to type the total number of values they are providing and then enter each value;
There is a way to just receive values only and not ask user to enter the total n. For brevity, I will skip it, but if you must know how to do it, let me know.
The point is if you want to store multiple values in a variable, you use loop and array.