Hi I am having difficulty with printing only 20 values in my loop. It should only repeat the loop 20 times but mine is repeating it 21 times. Can someone look over my code and tell me what I am doing wrong?
#include <iostream>
#include <iomanip>
usingnamespace std;
intconst MAX_VALUES = 20;
void programInfo();
void inputList(double numbers[], int & count);
int main()
{
int count = 0;
double numbers[MAX_VALUES];
programInfo();
cout << "\n";
cout << fixed << showpoint << setprecision(1);
inputList(numbers,count);
return 0;
}
void programInfo()
{
cout << "\n";
cout << "Please follow instructions carefully." << endl;
cout << "Enter one value at a time (at least 3 and up to 20)." << endl;
cout << "You must enter valid data or program will not work." << endl;
cout << "Enter -1 to signal end of data (-1 or -1.0)." << endl;
}
void inputList(double numbers[], int & count)
{
count = 0;
double value;
cout << "Please enter a value -->";
cin >> value;
while (value != -1.0 && count < MAX_VALUES)
{
numbers[count] = value;
count = count + 1;
cout << "Please enter a value -->";
cin >> value;
}
cout << "\n";
cout << "There are " << count << " values in the data set." << endl;
if (count < 3)
{
cout << "Error: You entered less than 3 values." << endl;
cout << "Please enter more values." << endl;
}
}