http://cboard.cprogramming.com/showthread.php?t=57546
Printable View
The counters are working.. Thanks... But the loop won't let me enter a choice twice... it goes straight to step 3 even though I enter 1. Why is that?
Code:#include <stdio.h>
#include <math.h>
#define strt_blnce 2000.00
#define yrly_intrst .02
int main (int argc, char **argv){
float dpst, withdrw, withdrw2, blnce, intrst,
finalblnce,blnce2;
int date, opt, crdt, dbt;
while (opt != 4){
printf("Your current balance is $%1.2f \n", strt_blnce);
printf("Banking program \n");
printf("1.) Deposit funds (credit transaction) \n");
printf("2.) Withdraw funds (debit transaction) \n");
printf("3.) Print statement of the acct \n");
printf("4.) Compute interest and exit program \n");
printf("Please indicate you option: ");
scanf("%d",&opt);
switch(opt){
case 1:
crdt = 0;
printf("Please enter a valid date from 1 to 31: ");
scanf("%d", &date);
printf("How much do you want to deposit? ");
scanf("%f", &dpst);
blnce = strt_blnce + dpst;
printf("Your current balance is $%1.2f. \n", blnce);
printf("Please indicate your option: ");
scanf("%d", &opt);
crdt++;
case 2:
dbt = 0;
printf("Please enter a valid date from 6 to 31: ");
scanf("%d", &date);
printf("How much do you want to withdraw? ");
scanf("%f", &withdrw);
if (withdrw > blnce) {
printf("Please enter a smaller amt. \n");
printf("How much do you want to withdraw? ");
scanf("%f", &withdrw);
}else
blnce2 = blnce - withdrw;
printf("Your current balance is $%1.2f. \n", blnce2);
dbt++;
printf("Please indicate your option: ");
scanf("%d", &opt);
case 3:
printf("Please enter a valid date from 10 to 31: ");
scanf("%d", &date);
printf("Your current balance is $%1.2f. \n", blnce);
printf("Number of credit transactions: %d \n ", crdt);
printf("Number of debit trasactions: %d \n ", dbt );
printf("Please indicate your option: ");
scanf("%d", &opt);
case 4:
printf("Statement of Account for October, 2004 \n");
printf("Number of credit transactions: %.2d \n ", crdt);
printf("Number of debit transactions: %.2d \n ", dbt);
intrst = blnce * yrly_intrst * 12;
printf("Interest computed: $%1.2f \n", intrst);
printf("Interest computed: $%1.2f \n", intrst);
finalblnce = blnce + intrst;
printf(" Final Balance: $%10.2f \n", finalblnce);
printf("Good Bye!! \n");
}
}
return 0;
}
Off the top of my head, I would say perhaps it is the scanf() function picking up whatever is left in stdin.
How would the scanf be affecting it?
If a call to scanf() does not take all the input from the stdin, the input just waits there until next time. So for example if you called scanf() which expected a type int, but the user entered a float number, scanf() would only read up to the decimal point, and leave the decimal pint and whatever is after the decimal in the stdin. Then, the next time scanf() is called, it reads what is left in the buffer first.
Actually, having taken a look at your program, I see you failed to add break; statements to your switch - so execution 'falls through' to each case. Add a break at the end of each case.
edit:
Since you have everything in a loop now, you may as well get rid of these statements too:
Code:case 1:
crdt = 0;
printf("Please enter a valid date from 1 to 31: ");
scanf("%d", &date);
printf("How much do you want to deposit? ");
scanf("%f", &dpst);
blnce = strt_blnce + dpst;
printf("Your current balance is $%1.2f. \n", blnce);
printf("Please indicate your option: ");
scanf("%d", &opt);
crdt++;
When I enter the breaks it wants to stop after option 3 and reprint the menu...
tam, maybe you can get together with Mackology101, since it appears yall are working on the same program (maybe even same class/school). 2 heads are better than 1
tammeyore....from glancing at your program it is not really stuctured correctly....i'm not sure how your program is supposed to flow, but consider losing the switch-case and making each case into a function if you know how to create functions...
the switch statement is fine. What you need to ditch is the scanf at the end of each case statement. And you need to put a break statement at the end of each case block, otherwise it will fall through. Go read up on how to use switch.
Without the scanf it won't let me enter another option.. And with the breaks it keeps printing the menu every time.
Thats the whole idea. After each transaction, the menu should be printed. If thats not supposed to happen, then print the menu before entering the while loop.
Also, the scnaf after the menu is printed will get the input for each run through the loop.
I put the menu outside of the loop but now option 4 is not showing.. I tried to add a do but did not like it. gives me
bank.c: In function `main':
bank.c:32: parse error before '{' token
Post the latests changes you made. I can't tell what you did wrong without seeing it.
Sorry.. Here you go...
Code:#include <stdio.h>
#include <math.h>
#define strt_blnce 2000.00
int main (int argc, char **argv){
float dpst, withdrw, withdrw2, blnce2, blnce, blnce3, intrst,
finalblnce;
int date, opt, crdt = 0, dbt = 0;
printf("Your current balance is $%1.2f \n", strt_blnce);
printf("Banking program \n");
printf("1.) Deposit funds (credit transaction) \n");
printf("2.) Withdraw funds (debit transaction) \n");
printf("3.) Print statement of the acct \n");
printf("4.) Compute interest and exit program \n");
printf("Please indicate your option: ");
scanf("%d",&opt);
while ( opt != 4) {
switch(opt){
switch(opt){
case 1:
printf("Please enter a valid date from 1 to 31: ");
scanf("%d", &date);
printf("How much do you want to deposit? ");
scanf("%f", &dpst);
if ( crdt == 0 ) {
blnce = strt_blnce + dpst;
} else if (crdt > 0) {
blnce3 = blnce2 + dpst;
}
printf("Your current balance is $%1.2f. \n\n", blnce);
crdt++;
printf("Please indicate your option: ");
scanf("%d", &opt);
break;
case 2:
printf("Please enter a valid date from 6 to 31: ");
scanf("%d", &date);
printf("How much do you want to withdraw? ");
scanf("%f", &withdrw);
if (withdrw > blnce) {
printf("Please enter a smaller amt. \n");
printf("How much do you want to withdraw? ");
scanf("%f", &withdrw);
}else
blnce2 = blnce - withdrw;
blnce = blnce2;
printf("Your current balance is $%1.2f. \n\n", blnce2);
dbt++;
printf("Please indicate your option: ");
scanf("%d", &opt);
break;
case 3:
printf("Please enter a valid date from 10 to 31: ");
scanf("%d", &date);
printf("Your current balance is $%1.2f. \n", blnce);
printf("Number of credit transactions: %d \n ", crdt);
printf("Number of debit trasactions: %d \n\n ", dbt );
printf("Please indicate your option: ");
scanf("%d", &opt);
break;
case 4:
printf("Statement of Account for October, 2004 \n");
printf("Number of credit transactions: %.2d \n ", crdt);
printf("Number of debit transactions: %.2d \n ", dbt);
intrst = (blnce * .02 * 12);
printf("Interest computed: $%.2f \n", intrst);
finalblnce = blnce + intrst;
printf(" Final Balance: $%1.2f \n", finalblnce);
printf("Good Bye!! \n");
break;
}
}
return 0;
}
I got case 4 to work but my calculations are totally off... Help???