For looping problem

I can't figure out why this is't looping. refer to annotation:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iomanip>


using namespace std;

string createCustID(); 	
int createAccount(); 
double printAccount(string& newID, double& deposit, int& account); 
 
		struct customerAccounts
			{ int account;
		
			};
		struct customerBalances
			{ double deposit;
			
			};


string fname;
string lname;
int accountNumber;
double deposit;
string newID = "";
int account = 0;
double finalprint;

int main()
{

			
	newID = createCustID(); 
	account = createAccount();
	finalprint =  printAccount(newID, deposit, account); 


	

	system("PAUSE");

	return 0;
}
	
string createCustID()
{
	string newID = "";
	
	cout << "Enter the customer's first name: " << endl;
	cin >> fname;

	cout << "Enter the customer's last name: " << endl;
	cin >> lname;

	newID = fname[0]+lname.substr(0, 4);
	cout << newID;
	
	return newID;
}


int createAccount () 
{
  accountNumber = 0;
  int nCustomer = 5;
  int customerAcc[5];
double customerBal[5];


	
	for (int customer = 0; customer < nCustomer; customer++)// i cant figure out why this isn't looping, so that it is asking for different account numbers and deposits.
		 { 
			 cout << endl << "Enter the numerical account number(must be above Zero): " << endl;
		cin >> accountNumber;

		cout << "Enter your initial deposit: " << endl;
		cin >> deposit;

			 if (deposit >= 0 && accountNumber > 0)
		
			{
	
				account = accountNumber;
				customerAcc[customer] = account;
				customerBal[customer] = deposit;
				return account; 
				return deposit;
			}
			
	else 
	{ cout << "Invalid account number or deposit." << endl;
	return 0;
			 }
	
	}}

double printAccount(string& newID, double& deposit, int& account)
{

const int nCustomer = 5;
int customerAcc[5];
double customerBal[5];


	
	for (int customer = 0; customer < nCustomer; customer++)
		if (deposit >= 0 && accountNumber > 0)
	{
		cout << endl << "**Account: ";
		cout << newID << account << " **Balance:  $";
		cout.precision(2);
		cout << fixed << deposit << endl;}
	return 0;
}
First, I just want to mention that you shouldn't start a new thread when it is the same code. Just keep the original thread going to ask different questions about the same code.

I don't think you understood what I said about the return statement, read what I said again, and answer this - what does it do?

Some other ideas:

Why don't you have one struct for an account type that has everything in it - CustID, AccountNum, names, balance etc. Then have one array of structs. Have one function that sets everything for a customer. Have it loop so that it fills the array with all the customers.

You still have global variables which is bad. Did you read up about references?

Another concept is scope of variables. A variable only lasts until the closing brace, so the nCustomer on line 102, only lasts until line 116 - then it ceases to exist. So line 102 should be in main.

The printAccount function doesn't need to return anything, so make it void on line 99.

Your braces are a bit weird, lines 108 to 115 should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
	
        for (int customer = 0; customer < nCustomer; customer++) {
		if (deposit >= 0 && accountNumber > 0) {
		    cout << endl << "**Account: ";
		    cout << newID << account << " **Balance:  $";
		    cout.precision(2);
		    cout << fixed << deposit << endl;
                }
       }

	return 0;
}   //end of function


The brace I put in on line 2 isn't really necessary, but it might save you one day when you add more code to a for loop.

HTH
Last edited on
Topic archived. No new replies allowed.