Thread: Having trouble with counters

  1. #16

  2. #17
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    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;
    
    }

  3. #18
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Off the top of my head, I would say perhaps it is the scanf() function picking up whatever is left in stdin.

  4. #19
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    How would the scanf be affecting it?

  5. #20
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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++;
    Last edited by kermit; 10-07-2004 at 08:05 PM.

  6. #21
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    When I enter the breaks it wants to stop after option 3 and reprint the menu...

  7. #22
    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
    DrakkenKorin

    Get off my Intarweb!!!!

  8. #23
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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...

  9. #24
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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.

  10. #25
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Without the scanf it won't let me enter another option.. And with the breaks it keeps printing the menu every time.

  11. #26
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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.

  12. #27
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    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

  13. #28
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Post the latests changes you made. I can't tell what you did wrong without seeing it.

  14. #29
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    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;
    
    }

  15. #30
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    I got case 4 to work but my calculations are totally off... Help???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. [C] Best way to save matrix counters
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 03-24-2006, 05:02 PM
  4. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  5. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM