943,989 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1850
  • C++ RSS
Sep 4th, 2008
0

C++Help Positive Number Average program

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csaund1 is offline Offline
6 posts
since Sep 2008
Sep 4th, 2008
0

Re: C++Help Positive Number Average program

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

i--;
continue;
It'll work.
Reputation Points: 5
Solved Threads: 3
Light Poster
hiraksarkardg is offline Offline
27 posts
since Sep 2008
Sep 4th, 2008
0

Re: C++Help Positive Number Average program

One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}
Reputation Points: 5
Solved Threads: 3
Light Poster
hiraksarkardg is offline Offline
27 posts
since Sep 2008
Sep 4th, 2008
0

Re: C++Help Positive Number Average program

One thing I forgot...place it in "if" block...like
if(num<0)
{
cout<<......
i--;
continue;
}
Reputation Points: 5
Solved Threads: 3
Light Poster
hiraksarkardg is offline Offline
27 posts
since Sep 2008
Sep 4th, 2008
0

Re: C++Help Positive Number Average program

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csaund1 is offline Offline
6 posts
since Sep 2008
Sep 4th, 2008
0

Re: C++Help Positive Number Average program

It works now. Would mind explaining why?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csaund1 is offline Offline
6 posts
since Sep 2008
Oct 2nd, 2009
0

Re: C++Help Positive Number Average program

design an algorithms to fine an average of 5 numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Furqanz is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: function cause error!
Next Thread in C++ Forum Timeline: recursion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC