HELP!!

My program runs correctly except after it outputs the balances entered it also gives me some weird numbers following...for ex: 5.28287e-308 (where are these numbers coming from)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    double balance[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
    double sum = 0.0;
    
    for (int i=0; i<5; i=i+1)
    {
    	balance[i] = 0.0;
    }
	
	cout << "You will be asked to enter the balance of 5 savings accounts."<< endl;
	
	for (int i=0; i<5; i=i+1)
	{
		cout << "Enter balance of an account: ";
		cin >> balance[i];
	}
	cout << "Balances entered: " <<endl;
	
	for (int i=0; i<10; i=i+1)
	{
		cout<<balance[i]<<endl;
	}
	
	for (int i=0; i<5; i=i+1)
	{
		sum = sum + balance [i];
	}
	
	cout << "total: " << sum << endl;
	
    for(int i =0; i<5; i++) 
	{
		if (balance[i] < 500)
	{
		balance[i] -= 10;
	}
    
	}
	
	cout << "Apply $10 fee for accounts with balance below $500.";
	
	cout << "New balances: " << endl;
	
	for (int i=0; i<5; i++)
	{
		cout << balance[i] << endl;
	}
	
	system("pause"); 
	return 0;
} 
Hi idgaf12,

When using array in c++ you have to remember that an array does no bounds checking. Meaning if you have an array of 5 members and then you assign them 10 items it will randomly put whatever it wants in there. You have to make sure that you don't extend pass your array or you will get garbage values.

Line 24 should be: for (int i = 0; i <4; i = i+1)

Actually all of your for loops should be this way (remember 0 is a number) once you change line 24 you should see values you want to see.

I hope this made sense
MRangel is right.
Line 24 should be:
1
2
3
4
5
6
	
for (int i=0; i<5; i=i+1)
{
	cout<<balance[i]<<endl;
	sum = sum + balance [i];
}
Last edited on
Topic archived. No new replies allowed.