Please help me to understand the codes. Ty.

int ac;
double bb,tch,tcr,cl,nb;
cout<<"Enter Account Number (or -1 to end)";
cin>>ac;
while (ac!=-1)
{
cout<<"Enter Beginning Balance: ";
cin>>bb;
cout<<"Enter total charge: ";
cin>>tch;
cout<<"Enter total credits: ";
cin>>tcr
cout<<"Enter Credit limit: ";
cin>>cl;
nb=(bb+ch)-tcr;
cout<<"New Balance is: "<<nb<<endl;
if (nb>cl)
{
cout<<"Account: "<<ac<<endl;
cout<<"Credit Limit: "<<cl<<endl;
cout<<"Balance: "<<nb<<endl;
cout<<"Credit limit Exceeded"<<endl;
}
break;
}
getch();
}


This is the problem

Develop a C++ program that will determine whether a department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available:

a. Account number (an integer)
b. Balance at the beginning of the month
c. Total of all items charged by this customer this month
d. Total of all credits applied to this customer’s account this month
e. Allowed credit limit

The program should use a while statement to input each of these facts, calculate the new balance (= beginning balance + charges credits) and determine whether the new balance exceeds the customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the customer’s account number, credit limit, new balance and the message “Credit Limit Exceeded.”

Sample Screen Output:

Enter account number (-1 to end): 100
Enter beginning balance: 5394.78
Enter total charges: 1000.00
Enter total credits: 500.00
Enter credit limit: 5500.00
New balance is 5894.78
Account: 100
Credit limit: 5500.00
Balance: 5894.78
Credit Limit Exceeded.


Enter Account Number (or -1 to quit): 200
Enter beginning balance: 1000.00
Enter total charges: 123.45
Enter total credits: 312.00
Enter credit limit: 1500.00
New balance is 802.45



Enter Account Number (or -1 to quit): 300
Enter beginning balance: 500.00
Enter total charges: 274.73
Enter total credits: 100.00
Enter total credit limit: 800.00
New balance is 674.73


Enter Account Number (or -1to quit) :-1

Not much wrong from the stated purpose of the program; but since these lines:

1
2
cout<<"Enter Account Number (or -1 to end)";
cin>>ac;


happen before the while loop, there are no conditions that will cause the loop to exit.

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
int ac;
double bb,tch,tcr,cl,nb;
cout<<"Enter Account Number (or -1 to end)";
cin>>ac;
while (ac!=-1)
{
         cout<<"Enter Beginning Balance: ";
         cin>>bb;
         cout<<"Enter total charge: ";
         cin>>tch;
         cout<<"Enter total credits: "; 
         cin>>tcr;
         cout<<"Enter Credit limit: ";
         cin>>cl;
         nb=(bb+ch)-tcr;
         cout<<"New Balance is: "<<nb<<endl;

         if (nb>cl)
        {
                    cout<<"Account: "<<ac<<endl;
                    cout<<"Credit Limit: "<<cl<<endl;
                    cout<<"Balance: "<<nb<<endl;
                    cout<<"Credit limit Exceeded"<<endl;
         }
        break;  //why??
}
getch();   // not needed
}              // extra bracket 


After the account number is entered, there should be a check for -1 to break the loop and exit, so the user doesn't have to enter all the values for a non-existent account.
All problems are fixed :)
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
#include <iostream>
#include <cstdio>
using namespace std;

int ac=0;
char End;
double bb,tch,tcr,cl,nb,ch;
int main(void){
while (ac>=0){
cout<<"Enter Account Number: ";
cin>>ac;
cout<<"\nEnter Beginning Balance: ";
cin>>bb;
cout<<"\nEnter total charge: ";
cin>>tch;
cout<<"\nEnter total credits: ";
cin>>tcr;
cout<<"\nEnter Credit limit: ";
cin>>cl;
nb=(bb+ch)-tcr;
cout<<"\nNew Balance is: "<<nb<<endl;
if (nb>cl)
{
cout<<"Account: "<<ac<<endl;
cout<<"Credit Limit: "<<cl<<endl;
cout<<"Balance: "<<nb<<endl;
cout<<"Credit limit Exceeded"<<endl;
}
cout<<"End? Y or N?\n> ";
cin>>End;
if(End=='Y'){ac=-1;}
}
}
Last edited on
All problems are fixed :)


Well, maybe not. . .

How does the user enter the second, third, etc. account number ( ac ) without restarting the program?

Your while loop will continue to ask for bb, tch, tcr, cl and perform the necessary calculations, but you can't get back to enter a different ac value this way!

in fact, after it enters the loop you can never modify ac to enter -1 and end the program, hence your lines 29 to 31.

I would put the "while" before line 9, then perform the required entries and calculations only if ac is greater than zero. This means, to make sure the loop starts correctly, that ac would be initialized to zero.

something like:

1
2
3
4
5
6
7
int ac = 0   // Account number
while (ac != -1)
{
            // user to enter a positive account number (-1 to end)
            // if account number is > 0
            //        { do the inputs and math }
}
Topic archived. No new replies allowed.