Thread: whats wrong with this?

  1. #31
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    I've heard scanf is a bad one to use but I've only being learning c code for about 4 weeks or so, this is one of my first pieces of code.

    Anyhow it doesn't really matter now, with the spelling mistake fixed (still can't belive i missed it) everything is hunky dorey, I've even got that hang of pointers now.

    You guys will be the first to get the version I'm gonna hand in belive me

  2. #32
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    I finally think that I've finished, here is the completed code that i will hand in on thursday, if anyone can find any obvious bugs (i.e maths being wrong, spelling mistakes) then please tell me so i can fix them.

    I just wanna thank you guys, i doubt that i could have completed this without you lot, i hope that my spelling will pick up the more i code

    Anyway on with the code:

    Code:
    #include <stdio.h>
    #include<time.h>
    
    float deposited (float, float*);
    float withdrawn (float, float*);
    
    int main()
    {
    
    int menu_choice = 0;
    int transactions = 0;
    float balance = 0;
    
    int nodeposits = 0;
    int nowithdraws = 0;
    float totaldeposited = 0;
    float totalwithdrawn = 0;
    time_t timer;
    while (menu_choice != 4)
    
    {
    printf ("Your current balance = %.2f \n", balance);
    printf ("1.  Deposit funds \n");
    printf ("2.  Withdraw funds \n");
    printf ("3.  Show balance \n");
    printf ("4.  Quit Program \n");
    printf ("Please select what you wish to do (1-4): \n");
    scanf ("%d", &menu_choice);
    
    
    timer = time (NULL);
    
    switch (menu_choice)
          {
          case 1:
          printf ("1. Deposit funds \n");
          balance = deposited (balance, &totaldeposited);
          nodeposits ++;
          transactions ++;
    
          printf("Deposit completed %s \n", asctime(localtime (&timer)));
          break;
    
          case 2:
          printf ("2. Withdraw funds \n");
          balance = withdrawn (balance, &totalwithdrawn);
          transactions ++;
          nowithdraws ++;
          printf("Withdraw completed %s \n", asctime(localtime (&timer)));
          break;
    
          case 3:
          printf ("3.  Show balance \n");
          printf ("Your current balance at %s is %.2f \n", asctime(localtime (&timer)), balance);
          break;
    
          case 4:
          printf ("Exiting... \n");
          printf ("closing balance.............%.2f \n", balance);
          printf ("completed transactions......%2d \n", transactions);
          printf (" \n");
          printf ("completed deposits..........%2d \n", nodeposits);
          printf ("total of money deposited....%.2f \n", totaldeposited);
          printf (" \n");
          printf ("completed withdraws.........%2d \n", nowithdraws);
          printf ("total of money withdrawn...%.2f \n", totalwithdrawn);
    
          break;
    
          default: 
          printf("Please Enter a Choice from 1-4\n" );
          break;
          }
    
    }
    
    
    return 0;
    
    }
    
    
    float deposited (float balance, float *ptr_totaldeposited)
    { 
      float deposit = 0;
      printf ("Please enter how much you wish to deposit \n");
      scanf ("%f", &deposit);
      if ((deposit + balance < 999999) || (deposit + balance > 0))
      {
        balance = balance + deposit;
        (*ptr_totaldeposited) = (*ptr_totaldeposited) + deposit;
        printf ("Thank you %.2f has been entered into your account\n", deposit);
      }
      else
        printf ("I'm sorry but the number you have entered out of range \n");
      
      return (balance);
    }
    
    
    
    
    
    float withdrawn (float balance, float *ptr_totalwithdrawn)
    { 
    float cashwithdraw = 0, chequewithdraw = 0;
    int transtype;
    
    printf ("Please enter how you wish to withdraw \n");
    printf ("1.   Cash \n");
    printf ("2.   Cheque\n");
    scanf ("%d", &transtype);
    
    switch (transtype)
        {
                case 1:
                {
                printf ("How much would you like to withdraw?  \n");
                scanf ("%f", &cashwithdraw);
                balance = balance - cashwithdraw;
                (*ptr_totalwithdrawn) = (*ptr_totalwithdrawn) + cashwithdraw;
                printf ("%.2f has being withdrawn from your account \n", cashwithdraw);
                break;
                }
    
                case 2:
                {
                printf ("How much would you like to withdraw? (maximum of 500) \n");
                scanf ("%f", &chequewithdraw);
    
    
                if ((chequewithdraw > 500) || (chequewithdraw < 0))
                printf ("You are limited to a maximum of 500 \n");
    
                {
                printf ("%.2f has being withdrawn from your account \n", chequewithdraw);
                balance = balance - chequewithdraw;
                (*ptr_totalwithdrawn) = (*ptr_totalwithdrawn) + cashwithdraw;
                }
                break;
    
                default: 
                printf("Please Enter a Choice from either 1 or 2\n" );
                break;
    
                }
        }
    
    
    
    return (balance);
    }

  3. #33
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >If someone had just said line 3 of the code i would of found it

    Well I tried to do so using bold red text and other not-so-subtle hints, but...

    Anyways, I think I like this bank.
    Code:
    Your current balance = 0.00
    1.  Deposit funds
    2.  Withdraw funds
    3.  Show balance
    4.  Quit Program
    Please select what you wish to do (1-4):
    1
    1. Deposit funds
    Please enter how much you wish to deposit
    10
    Thank you 10.00 has been entered into your account
    Deposit completed Tue Jan 06 22:00:48 2004
    
    Your current balance = 10.00
    1.  Deposit funds
    2.  Withdraw funds
    3.  Show balance
    4.  Quit Program
    Please select what you wish to do (1-4):
    2
    2. Withdraw funds
    Please enter how you wish to withdraw
    1.   Cash
    2.   Cheque
    1
    How much would you like to withdraw?
    10000
    10000.00 has being withdrawn from your account
    Withdraw completed Tue Jan 06 22:00:54 2004
    
    Your current balance = -9990.00
    1.  Deposit funds
    2.  Withdraw funds
    3.  Show balance
    4.  Quit Program
    Please select what you wish to do (1-4):
    4
    Exiting...
    closing balance.............-9990.00
    completed transactions...... 2
    
    completed deposits.......... 1
    total of money deposited....10.00
    
    completed withdraws......... 1
    total of money withdrawn...10000.00
    You might want to check whether the funds are available before handing out "money".


    Oh, and you might want to take some time and indent nicely. It makes things more readable, but it also helps pick out unintentional things, some of which may be bugs.
    Code:
    float withdrawn (float balance, float *ptr_totalwithdrawn)
    {
       float cashwithdraw = 0, chequewithdraw = 0;
       int transtype;
    
       printf ("Please enter how you wish to withdraw \n");
       printf ("1.   Cash \n");
       printf ("2.   Cheque\n");
       scanf ("%d", &transtype);
    
       switch ( transtype )
       {
       case 1:
          {
             printf ("How much would you like to withdraw?  \n");
             scanf ("%f", &cashwithdraw);
             balance = balance - cashwithdraw;
             (*ptr_totalwithdrawn) = (*ptr_totalwithdrawn) + cashwithdraw;
             printf ("%.2f has being withdrawn from your account \n", cashwithdraw);
             break;
          }
    
       case 2:
          {
             printf ("How much would you like to withdraw? (maximum of 500) \n");
             scanf ("%f", &chequewithdraw);
    
    
             if ( (chequewithdraw > 500) || (chequewithdraw < 0) )
                printf ("You are limited to a maximum of 500 \n");
    
             {
                printf ("%.2f has being withdrawn from your account \n", chequewithdraw);
                balance = balance - chequewithdraw;
                (*ptr_totalwithdrawn) = (*ptr_totalwithdrawn) + cashwithdraw;
             }
             break;
    
             default: 
                printf("Please Enter a Choice from either 1 or 2\n" );
                break;
    
          }
       }
    
       return (balance);
    }
    Last edited by Dave_Sinkula; 01-06-2004 at 10:41 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM