Floating avg and fatal error

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

Join Date: Oct 2008
Posts: 18
Reputation: Dontais is an unknown quantity at this point 
Solved Threads: 0
Dontais's Avatar
Dontais Dontais is offline Offline
Newbie Poster

Floating avg and fatal error

 
0
  #1
Oct 28th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 332
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 64
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Floating avg and fatal error

 
0
  #2
Oct 28th, 2008
  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. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Floating avg and fatal error

 
0
  #3
Oct 28th, 2008
[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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 18
Reputation: Dontais is an unknown quantity at this point 
Solved Threads: 0
Dontais's Avatar
Dontais Dontais is offline Offline
Newbie Poster

Re: Floating avg and fatal error

 
0
  #4
Oct 28th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Floating avg and fatal error

 
0
  #5
Oct 29th, 2008
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Floating avg and fatal error

 
0
  #6
Oct 29th, 2008
Originally Posted by WaltP View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



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