C++Help Positive Number Average program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

C++Help Positive Number Average program

 
0
  #1
Sep 4th, 2008
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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. // Variables
  7. const int positive_numbers = 10;
  8. int numbers[positive_numbers];
  9. double average;
  10. double sum = 0.0;
  11. int num;
  12. //Read all numbers
  13. for (int i = 0; i < positive_numbers; i++)
  14. {
  15. cout << "Enter a number: ";
  16. cin >> num;
  17. if( num < 0 ) cout << "Invalid! Please enter a number greater than or equal to zero." << endl;
  18. numbers[i] = num;
  19. sum += numbers[i];[/B]
  20. }
  21.  
  22. //Find the average
  23. average = sum/10;
  24.  
  25. // Display average
  26. cout <<"\nThe average is " << average <<"\n\n";
  27.  
  28.  
  29. return 0;
  30. }
Last edited by Narue; Sep 4th, 2008 at 1:21 pm. Reason: add
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 26
Reputation: hiraksarkardg has a little shameless behaviour in the past 
Solved Threads: 2
hiraksarkardg hiraksarkardg is offline Offline
Light Poster

Re: C++Help Positive Number Average program

 
0
  #2
Sep 4th, 2008
That's very simple one. Just write the codes below after line 17.

i--;
continue;
It'll work.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 26
Reputation: hiraksarkardg has a little shameless behaviour in the past 
Solved Threads: 2
hiraksarkardg hiraksarkardg is offline Offline
Light Poster

Re: C++Help Positive Number Average program

 
0
  #3
Sep 4th, 2008
One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 26
Reputation: hiraksarkardg has a little shameless behaviour in the past 
Solved Threads: 2
hiraksarkardg hiraksarkardg is offline Offline
Light Poster

Re: C++Help Positive Number Average program

 
0
  #4
Sep 4th, 2008
One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

Re: C++Help Positive Number Average program

 
0
  #5
Sep 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

Re: C++Help Positive Number Average program

 
0
  #6
Sep 4th, 2008
It works now. Would mind explaining why?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: Furqanz is an unknown quantity at this point 
Solved Threads: 0
Furqanz Furqanz is offline Offline
Newbie Poster

Re: C++Help Positive Number Average program

 
0
  #7
Oct 2nd, 2009
design an algorithms to fine an average of 5 numbers
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC