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
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.
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 }
}