| | |
Floating avg and fatal error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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)
int main () { double num, sum, average; cout << "Enter a value -99 to end:"; cin >> num; while (num != -99) { sum = sum + num; average = sum/num; cout << "The average is:" << setprecision (2) << (double)average << endl << endl; cout << "Enter another value -99 to end:"; cin >> num; } return 0; }
c++ Syntax (Toggle Plain Text)
int main () { double num = 0.0, sum = 0.0, average=0.0; cout << "Enter a value -99 to end:"; cin >> num; while (num != -99) { sum = sum + num; average = sum/num; cout << "The average is:" << setprecision (2) << (double)average << endl << endl; cout << "Enter another value -99 to end:"; cin >> num; } return 0; }
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
[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.
That should do it.
Thanx...
Sean
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)
int main() { double num = -99, sum = 0, average = 0, count = 0; std::cout << "Enter a value -99 to end:"; std::cin >> num; while (num != -99) { count++; sum += num; average = sum/count; std::cout << "The average is:" << setprecision (2) << (double)average << endl << endl; cout << "Enter another value -99 to end:"; cin >> num; } return 0; }
That should do it.
Thanx...
Sean
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
•
•
•
•
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: Pass and Return a string.
- Next Thread: Question regarding new/delete vs. malloc/free
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






