954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++Help Positive Number Average program

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;
//Read all numbers
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;
}
csaund1
Newbie Poster
6 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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

i--;
continue;
It'll work.

hiraksarkardg
Light Poster
27 posts since Sep 2008
Reputation Points: 5
Solved Threads: 3
 

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

hiraksarkardg
Light Poster
27 posts since Sep 2008
Reputation Points: 5
Solved Threads: 3
 

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

hiraksarkardg
Light Poster
27 posts since Sep 2008
Reputation Points: 5
Solved Threads: 3
 

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.

csaund1
Newbie Poster
6 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

It works now. Would mind explaining why?

csaund1
Newbie Poster
6 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

design an algorithms to fine an average of 5 numbers

Furqanz
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You