Basic credit Limit

closed account (iyRG3TCk)
After the first customer's case It should display the "Enter the account number: "(of 2nd customer)..but it displays "Enter the beginning balance:".. I know the issue is in my for loop or while loop but I can't find what is the issue plz help..here is the code

#include<iostream>
using namespace std;

double cal(double,double, double, double,double);
int accountNo;
double Bbalance;
double Nbalance;
double charges;
double credits;
double climit;
int main() {
for (int i = 0;; ++i) {
cout << "Enter account number (or -1 to quit) :";
cin >> accountNo;

while (accountNo > 0) {
cout << "Enter the begining balance: ";
cin >> Bbalance;
cout << "Enter total charges: ";
cin >> charges;
cout << "Enter total credits: ";
cin >> credits;
cout << "Enter the credit limit: ";
cin >> climit;

cal(accountNo, Bbalance, charges, credits, climit);

}

}
system("pause");

}

double cal(double,double, double, double, double) {

Nbalance = Bbalance + charges - credits;
if (Nbalance > climit) {
cout << "New balance is: " << Nbalance<<endl;
cout << "Account: " << accountNo << endl;
cout << "Credit limit: " << climit << endl;
cout << "\n Credit limit Exceeded!" << endl;
}
else {
cout << "New balance is: " << Nbalance << endl;
}
return 0;
}
I don't understand what purpose the while loop has. It will repeat as long as accountNo is greater than zero, but accountNo is never modified within the loop so it will go on forever.
closed account (iyRG3TCk)
Actually I need to exit the program after the user input no: -1 as his account no: so that's why I used while.Please I'm really new to these stuffs so if u help me it will be a really helpful...thnx !
To quit the program when the accountNo is -1, do like this
1
2
3
4
5
6
7
for (int i = 0;; ++i) {
    cout << "Enter account number (or -1 to quit) :";
    cin >> accountNo;
    if(accountNo < 0) break; // this breaks the for loop and nothing below will run
    // if account >= 0, then program will proceed
    // ....
}


You do not need the while loop
My lecturer advices us strongly against the use of global variables except it is a constant

It is also good to start variable names with lowercase letters.
So if i were the one, i would code the program like this.

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

double calculateBalance(int, double, double, double, double);

int main() 
{
	// declare and initialize variables
	int  accountNo = 0;
	double begBalance = 0.0;
	double charges    = 0.0;
	double credits    = 0.0;
	double credLimit   = 0.0;

	for (int i = 0; ; ++i) {
		cout << "Enter account number (or -1 to quit) :";
		cin >> accountNo;

		if (accountNo < 0) break; // or if(accountNo == -1) break;

		cout << "Enter the begining balance: ";
		cin >> begBalance;

		cout << "Enter total charges: ";
		cin >> charges;

		cout << "Enter total credits: ";
		cin >> credits;

		cout << "Enter the credit limit: ";
		cin >> credLimit;

		// do balance calculation here
	}
	return 0;
}
double cal(int  accountNo, double begBalance, double charges, double credits, double climit) 
{
	double Nbalance = begBalance + charges - credits;
        // ...
}


Hope this helps
Last edited on
closed account (iyRG3TCk)
#Blongho Thnx! that was very very helpful. :D
Topic archived. No new replies allowed.