Автор: Alkor
Дата сообщения: 04.11.2005 21:24
Нужна помощь с while loop . Вот код и в main() функции по непонятной причине сначала запускается код в else а потом только спрашивает ввод, getTrans().
Код: #include <stdio.h>
void getBalance(double *p_balance);
void transMenu();
char getTrans();
void updateCheque(double *p_balance, double *p_cheque, int *p_cheques, int *p_bounced);
void updateDeposit(double *p_balance, double *p_deposit, int *p_deposits);
void updateWithdrawal(double *p_balance, double *p_withdrawal, int *p_withdrawals);
void updateServiceCharge(double *p_balance, double *p_serviceCharge, int *p_cheques, int *p_bounced, int *p_deposits, int *p_withdrawals);
int main()
{
int bounced = 0, deposits = 0, cheques = 0, withdrawals = 0;
double balance, serviceCharge, cheque, deposit, withdrawal;
char trans;
getBalance(&balance);
transMenu();
while((trans = getTrans()) != 'q' && trans !='Q')
{
if (trans == 'c' || trans == 'C'){
updateCheque(&balance, &cheque, &cheques, &bounced);
transMenu();
}
else if(trans == 'd' || trans == 'D'){
updateDeposit(&balance, &deposit, &deposits);
transMenu();
}
else if(trans == 'w' || trans == 'W'){
updateWithdrawal(&balance, &withdrawal, &withdrawals);
transMenu();
}
else
printf("Invalid input!\n");
}
updateServiceCharge(&balance, &serviceCharge, &cheques, &bounced, &deposits, &withdrawals);
getchar();
getchar();
return 0;
}
void updateServiceCharge(double *p_balance, double *p_serviceCharge, int *p_cheques, int *p_bounced, int *p_deposits, int *p_withdrawals)
{
*p_serviceCharge = (*p_cheques)*1.5+(*p_bounced)*15.0+(*p_deposits)*0.5+(*p_withdrawals)*0.5;
*p_balance = *p_balance - *p_serviceCharge;
printf("\n");
printf("Transaction Debit Credit Balance\n");
printf("****************************************************\n");
printf("Cheque %.2lf %.2lf\n", *p_serviceCharge, *p_balance);
printf("****************************************************\n\n");
}
void updateWithdrawal(double *p_balance, double *p_withdrawal, int *p_withdrawals)
{
printf("Enter amount of withdrawal: ");
scanf("%lf", p_withdrawal);
if(*p_withdrawal <= *p_balance && *p_withdrawal>=0){
*p_balance = *p_balance - *p_withdrawal;
*p_withdrawals = *p_withdrawals + 1;
printf("\n");
printf("Transaction Debit Credit Balance\n");
printf("****************************************************\n");
printf("Cheque %.2lf %.2lf\n", *p_withdrawal, *p_balance);
printf("****************************************************\n\n");
}
else{
printf("Invalid amount!\n");
updateWithdrawal(p_balance, p_withdrawal, p_withdrawals);
}
}
void updateDeposit(double *p_balance, double *p_deposit, int *p_deposits)
{
printf("Enter amount of deposit: ");
scanf("%lf", p_deposit);
if(*p_deposit>=0){
*p_balance = *p_balance + *p_deposit;
*p_deposits = *p_deposits + 1;
printf("\n");
printf("Transaction Debit Credit Balance\n");
printf("****************************************************\n");
printf("Cheque %.2lf %.2lf\n", *p_deposit, *p_balance);
printf("****************************************************\n\n");
}
else{
printf("Invalid amount!\n");
updateDeposit(p_balance, p_deposit, p_deposits);
}
}
void updateCheque(double *p_balance, double *p_cheque, int *p_cheques, int *p_bounced)
{
printf("Enter amount of cheque: ");
scanf("%lf", p_cheque);
if(*p_cheque<=*p_balance && *p_cheque>0){
*p_balance = *p_balance - *p_cheque;
*p_cheques = *p_cheques + 1;
printf("\n");
printf("Transaction Debit Credit Balance\n");
printf("****************************************************\n");
printf("Cheque %.2lf %.2lf\n", *p_cheque, *p_balance);
printf("****************************************************\n\n");
}
else if(*p_cheque>*p_balance){
printf("The amount is greater than balance!\n");
*p_bounced = *p_bounced + 1;
}
else{
printf("Invalid input!\n");
updateCheque(p_balance, p_cheque, p_cheques, p_bounced);
}
}
void getBalance(double *p_balance)
{
printf("Welcome to the Cheque Book Balancer\n\n");
printf("Please enter the current balance: ");
scanf("%lf", p_balance);
printf("\n");
printf("*******************************************************\n");
printf("Balance forward %.2lf\n", *p_balance);
printf("*******************************************************\n\n");
}
char getTrans()
{
char ch;
scanf("%c", &ch);
return ch;
}
void transMenu()
{
printf("Transaction Menu\n");
printf("================\n");
printf("c - Cheque\n");
printf("d - Deposit\n");
printf("w - Withdrawal\n");
printf("q - Quit\n");
printf("================\n");
printf("Enter selection (c, d, w, or q): ");
}