This program kinda works, but I have one problem. I setup this program to display a message when someone enters a negative number, but I how do I set the count back to zero. Even though mesage prompts the user that a negative number is invalid, it still calculates the number in the average. Please help

``````#include <iostream>
using namespace std;

int main()
{
// Variables
const int positive_numbers = 10;
int numbers[positive_numbers];
double average;
double sum = 0.0;
int num;
for (int i = 0; i < positive_numbers; i++)
{
cout << "Enter a number: ";
cin >> num;
if( num < 0 ) cout << "Invalid! Please enter a number greater than or equal to zero." << endl;
numbers[i] = num;
sum += numbers[i];[/B]
}

//Find the average
average = sum/10;

// Display average
cout <<"\nThe average is " << average <<"\n\n";

return 0;
}``````

That's very simple one. Just write the codes below after line 17.

i--;
continue;
It'll work.

One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}

One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}

it now does not stop after ten numbers. The program let the user enter 10 number and outputs the average. It now seems like you can enter an infinite amount of numbers. when a user enter a negative number, I need the program to not calculate that number as part of the ten. any other suggestions.

It works now. Would mind explaining why?

design an algorithms to fine an average of 5 numbers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.