# Thread: working with arrays

1. ## working with arrays

I am writing a program that creates an int array, takes ints from the user, and determines the min, max and average of all values. This part I can do. What I am having trouble with is that the values entered can only be positive. So if the user enters a negative number I have to send an error message to the screen saying int must be positive, which I have done already. I can't figure out how to keep from counting that value in my counter and not include the negative number in my calculations for the minimum.

Here is the code:
Code:
``` #include <iostream>

using namespace std;

int main()
{
int nums[100];
int i, max, min, total =0, count = 1;
double avg;

cout << "Enter positive interger numbers (or -1 to quit): " << endl;
cin >> nums[0];
if (nums[0] == -1)
{
cout << "Bye.";
return 0;
}
if (nums[0] < -1)
cout << "********ERROR SKIPPED ENTRY********" << endl << "Number must be positive";

max = nums[0];
min = nums[0];
total = nums[0];

while (i > -1)
{
for (i = 1; i < 100; i++)
{
cin >> nums[i];

if (nums[i] < -1)
{
cout << "********ERROR SKIPPED ENTRY********" << endl << "Number must be positive";
count = count -1;
total = total - i;
}

if (nums[i] == -1)
{
cout << "Read in " << i << " values." << endl;
cout << "Minimum value: " << min << endl;
cout << "Maximum value: " << max << endl;
cout << "Average: " << (total +1) / i << endl;
cout << count << endl;
cout << total;
return 0;
}
else
{
if (nums[i] > max)
max = nums[i];

if (nums[i] < min)
min = nums[i];

count ++;
total = nums[i] + total;
}
}
}
return 0;
}```
Thanks for any help.

2. You could do a do/while loop like so:
Code:
```do
{
cin>>nums[i];
} while (nums[i]<-1);```

Popular pages Recent additions