The code currently takes an input of the number of data entries and an input of number separated by a single space. My aim is to get the data entered and convert it into an array however whenever I try using the format
int array[ndata];
The build contains errors due to the variable ndata not being constant. How do I change the input for the size of the array to allow for this?
Code:
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int MAXDATA = 100;
bool ok = 1;
double sum = 0, x, data[MAXDATA];
double min = 1e32, max = -1e32;
int ndata = 0;
int count = 0;
while (ok)
{
cout << "Enter number of data to input then press <Enter>: ";
cin >> ndata;
if (ndata > 0 && ndata < MAXDATA) ok = 0;
cout << "Enter numbers separated by a single space, then press <Enter>: ";
count = 0;
while (count < ndata)
{
cin >> x;
data[count] = x;
sum = sum + x;
count++;
cout << "sum is " << sum << endl;
}
}
}