943,514 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 623
  • C++ RSS
Oct 28th, 2008
0

Floating avg and fatal error

Expand Post »
I'm trying to run my program and when I enter say 3 it gives me a fatal error. It is suppose to calculate the avg of each value entered and output it. Would appreciate a little advise on this I'm stumped. The error says "warning C4700: uninitialized local variable 'sum' used".

I think I might haft to declare sum as an integer but that dosn't seem right if I want digits up to 2 decimal positions.

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. double num, sum, average;
  4.  
  5. cout << "Enter a value -99 to end:";
  6. cin >> num;
  7.  
  8. while (num != -99)
  9. {
  10. sum = sum + num;
  11. average = sum/num;
  12.  
  13. cout << "The average is:" << setprecision (2) << (double)average << endl << endl;
  14.  
  15. cout << "Enter another value -99 to end:";
  16. cin >> num;
  17. }
  18. return 0;
  19. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dontais is offline Offline
18 posts
since Oct 2008
Oct 28th, 2008
0

Re: Floating avg and fatal error

c++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. double num = 0.0, sum = 0.0, average=0.0;
  4.  
  5. cout << "Enter a value -99 to end:";
  6. cin >> num;
  7.  
  8. while (num != -99)
  9. {
  10. sum = sum + num;
  11. average = sum/num;
  12.  
  13. cout << "The average is:" << setprecision (2) << (double)average << endl << endl;
  14.  
  15. cout << "Enter another value -99 to end:";
  16. cin >> num;
  17. }
  18. return 0;
  19. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Oct 28th, 2008
0

Re: Floating avg and fatal error

[QUOTE=Dontais;723659]I'm trying to run my program and when I enter say 3 it gives me a fatal error. It is suppose to calculate the avg of each value entered and output it. Would appreciate a little advise on this I'm stumped. The error says "warning C4700: uninitialized local variable 'sum' used".

I think I might haft to declare sum as an integer but that dosn't seem right if I want digits up to 2 decimal positions.

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. double num = -99, sum = 0, average = 0, count = 0;
  4. std::cout << "Enter a value -99 to end:";
  5. std::cin >> num;
  6.  
  7. while (num != -99)
  8. {
  9. count++;
  10. sum += num;
  11. average = sum/count;
  12.  
  13. std::cout << "The average is:" << setprecision (2) << (double)average << endl << endl;
  14.  
  15. cout << "Enter another value -99 to end:";
  16. cin >> num;
  17. }
  18. return 0;
  19. }

That should do it.

Thanx...
Sean
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Oct 28th, 2008
0

Re: Floating avg and fatal error

Thanks Sean didn't realize to add an extra variable. I couldn't edit I figure the = 0.0 for the variables but thanks guys!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dontais is offline Offline
18 posts
since Oct 2008
Oct 29th, 2008
0

Re: Floating avg and fatal error

To all of you, floating point values are rarely exact. Therefore testing with == will rarely work.

For example, if you entered 46, the actual value stored in the variable might be 46.000001 or 45.9999997, neither of which is equal to 46.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Oct 29th, 2008
0

Re: Floating avg and fatal error

Click to Expand / Collapse  Quote originally posted by WaltP ...
To all of you, floating point values are rarely exact. Therefore testing with == will rarely work.

For example, if you entered 46, the actual value stored in the variable might be 46.000001 or 45.9999997, neither of which is equal to 46.

Correct, it is common to define a 'delta' which is the amount of error you wish to tolerate, and to correct the value as it drifts.

Thanx...
Sean
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

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: Pass and Return a string.
Next Thread in C++ Forum Timeline: Question regarding new/delete vs. malloc/free





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


Follow us on Twitter


© 2011 DaniWeb® LLC