/* Develop a C program that will determine if a department store customer has exceeded the credit limit
on a charge account. For each customer, the following facts are available:
a) account number
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 by this customer's account this month
e) Allowed credit limit
The program should input each of these facts. Calculate the new balance (= beginning balance + charges - credits),
and determine if 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." */
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
float account_number;
float beginning_balance;
float total_charges;
float total_credits;
float credit_limit;
float balance;
float counter;
/* intialization phase */
total_charges = 0;
total_credits = 0;
account_number = 0;
beginning_balance = 0;
counter = 0;
balance = 0;
credit_limit = 0;
while ( account_number != -1 )
{
printf( "Enter account number ( -1
to end ): \n" );
scanf( "%.2f", &account_number);
if ( account_number != -1 ) {
printf( "Enter beginning
balance: \n" );
scanf( "%.2f",
&beginning_balance);
printf( "Enter total charges:
\n" );
scanf( "%.2f", &total_charges);
printf( "Enter total credits:
\n" );
scanf( "%.2f", &total_credits );
printf( "Enter credit limit:
\n" );
scanf( "%.2f", &credit_limit);
system( "pause" );
}
if ( balance > credit_limit ) {
balance = beginning_balance +
total_charges - total_credits;
printf ( "Account: %.2f \n",
account_number);
printf ( "Credit limit: %.2f \n",
credit_limit);
printf ( "Balance: %.2f \n",
balance);
printf ( "Credit limit exceeded
\n" );
}
system ( "pause" );
return 0;
}
}
Can someone tell me what is wrong with the program because it is not working as it should!! Thanks!
3 answers
Sometimes the messages or symptoms are compiler dependent, so it is also a good idea to indicate the compiler you used.
There is no major problem with the programme. However, you may want to consider the following comments:
1. Account number is usually an integer, so I would declare it as a long type, so it will take up to 9 digits (depending on the compiler).
Do not forget to change the input and output formats to %d.
2. The input format for decimal (float) numbers are simply %f. The user can optionally type in the decimal point followed by as many digits as he wishes.
3. You may have extraneous "pauses", but perhaps you have put them in for debugging purposes.
4. The if statement for testing credit limit should be placed immediately after the calculations, and should include only the printing statement. The calculations should not be affected by the if statement.
5. In case the credit limit is exceeded (or not exceeded), you would print the credit limit remaining (or how much has been exceeded). This will give a confirmation for the client, as is done on credit card statements.
If your problem persists, do indicate the nature of the problem or the error message. The name of the compiler would often help as well.
using namespace std;
int main()
{
int account_number=0;
float beginning_balance=0;
float total_charges=0;
float total_credits=0;
float credit_limit=0;
float balance=0;
float counter=0;
while( account_number != -1 )
{
cout<<" Enter account number (-1 to end): "<<endl;
cin>>account_number;
if ( account_number != -1 )
{
cout<<" Enter beginning balance: "<<endl;
cin>>beginning_balance;
cout<<" Enter total charges: "<<endl;
cin>>total_charges;
cout<<" Enter total credits: "<<endl;
cin>>total_credits;
cout<<" Enter credit limit: "<<endl;
cin>>credit_limit;
}
if ( balance>credit_limit)
{
balance=beginning_balance + total_charges - total_credits;
cout<<" Account: "<<account_number<<endl;
cout<<" Credit Limits: "<<credit_limit<<endl;
cout<<" Balance: "<<balance<<endl;
cout<<" Credit Limit Exceeded";
}
}
}