954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Floating avg and fatal error

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.

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; 
}
Dontais
Newbie Poster
18 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
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; 
}
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

[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.

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

seanhunt
Light Poster
40 posts since Oct 2008
Reputation Points: 13
Solved Threads: 6
 

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!

Dontais
Newbie Poster
18 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

seanhunt
Light Poster
40 posts since Oct 2008
Reputation Points: 13
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You