creating and using a group of variables HELP!

Hi here is the problem, Write a program to do the following. Ask the user to enter the balance of 5 saving accounts. Store the data in an array. Then do the following:
(a) Display the account balances entered by the user.
(b) Calculate and display the total of the 5 accounts.
(c) Examine the account balances. If the balance of an account is below $500, charge a $10 fee to the account. Display all balances again. The following is an example.

Im getting lots of errors with my code, please help.

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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int balance[5] = {0, 0, 0, 0, 0};
    int sum = 0;

	for (int i=0; i<5; i=i+1)
	{
		cout << "You will be asked to enter the balance of 5 savings accounts. ";
		cin >> balance[i];
		
		sum = sum + balance[i];
	}
	
	cout<< "total: " << sum << endl;
	
	if (balance < 10)
	{
		balance = balance - 10;
	}
	
	cout << "Apply $10 fee for accounts with balance below $500.;
	cout << "New balances: " << endl;
	for (int i=0; i<5; i=i+1)
	{
		cout << balance[i] << endl;
	}
	
	system("pause"); 
	return 0;
} 
Line 20,22: You declared balance as an array at line 7. Here you're trying to use balance as a simple int. Your instructions say to examinne each balance. This would imply a loop.

Line 25: You're missing a closing "
right, but im not sure how to make the balance change based on if the balance is below 500$ with using a loop.
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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int balance[5] = {0, 0, 0, 0, 0};
    int sum = 0;

	for (int i=0; i<5; i=i+1)
	{
		cout << "You will be asked to enter the balance of 5 savings accounts. ";
		cin >> balance[i];
		
		sum = sum + balance[i];
	}
	
	cout<< "total: " << sum << endl;
	cout << endl;

	cout << "Apply $10 fee for accounts with balance below $500." << endl;
	for (int i=0; i<5; i= i+ 1 )
	{
		
		cout << "New balances: " << endl;
	}
	for (int i=0; i<5; i=i+1)
	{
		cout << balance[i] << endl;
	}
	
	system("pause"); 
	return 0;
}
24
25
  if (balance[i] < 500)
    balance[i] -= 10;


Note that balance is an array of ints. I don't know what currency you're assuming, but if you want to track fractional amounts (such as dollars and cents), you should consider using a array of doubles.

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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    double balance[5] = {0, 0, 0, 0, 0};
    int sum = 0;

	for (int i=0; i<5; i++)
	{
		cout << "You will be asked to enter the balance of 5 savings accounts. ";
		cin >> balance[i];
		
		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;
} 
Last edited on
I have this code and now im getting an error of the following: E:\CIS 115\Lab23P2.cpp In function 'int main()':
[Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
[Note] (if you use '-fpermissive' G++ will accept your code)


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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    double balance[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
    int sum = 0.0;

	for (int i=0; i<5; i++)
	{
		balance[i] = 0.0;
	}
	
	cout << "You will be asked to enter the balance of 5 savings accounts."<< endl;
	
	cout << "Enter balance of an account: ";
	cin >> 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;
} 
Line 18: i has gone out of scope at line 13. You want to get the balance of the 5 accounts, so you need a for loop here. This is the cause of the error you're getting.

lines 10-13 are not needed since you initialized the array to zeroes at line 7.

Line 8: sum should be a double.

Line 20: You never add the 5 balances to sum. sum will print as 0.
Topic archived. No new replies allowed.