whats wrong with this?

This is a discussion on whats wrong with this? within the C Programming forums, part of the General Programming Boards category; this is all very well and good but does anyone know why its stuck on 45 or 44? Unless its ...

  1. #16
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    this is all very well and good but does anyone know why its stuck on 45 or 44? Unless its just me, in which case tell me if everything is working fine

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i only just got the post and haven't got round to doing it yet
    Well, when you get around to doing it you can feel free to ask for more help. We don't like endless threads trying to get someone to fix an obvious error.

    >isn't that what i put?
    No, what you actually put was what I quoted.

    >this is all very well and good but does anyone know why its stuck on 45 or 44?
    Did you fix all of the errors and warnings? Because once I do that your code works just peachy in Dev-C++.
    My best code is written with the delete key.

  3. #18
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    um, i've fixed all those problems (i think) but its still not doing what i want, whenever i try to withdraw cash or cheques the answer always comes out to 45.00, why!?!?


    the code has being uploaded (again)
    http://petedeeuk.8bit.co.uk/wtf.jpg

    oh and why do windows close when the code is finished and how do i stop it?
    Last edited by petedee; 01-06-2004 at 02:54 PM.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ
    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.*

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why!?!?
    I can't help you with a problem I can't reproduce.

    >the code has being uploaded (again)
    Here is the code I have been using that works properly on both Dev-C++ and VC++. Does it match yours?
    Code:
    #include <stdio.h>
    
    float deposited (float);
    float withdrawn (float);
    
    int main()
    {
      int menu_choice = 0;
      int transactions = 0;
      float balance = 0;
      
      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);
        switch (menu_choice)
        {
        case 1:
          printf ("1. Deposit funds \n");
          balance = deposited (balance);
          transactions ++;
          break;
        case 2:
          printf ("2. Withdraw funds \n");
          balance = withdrawn (balance);
          break;
        case 3:
          printf ("3.  Show balance \n");
          printf ("Your current balance = %.2f \n", balance);
          break;
        case 4:
          printf ("Exiting... \n");
          break;
        default: 
          printf("Please Enter a Choice from 1-4\n" );
          break;
        }
      }
      
      return 0;
    }
    
    float deposited (float balance)
    { 
      float deposit;
      
      printf ("Please enter how much you wish to deposit \n");
      scanf ("%f", &deposit);
      if ((deposit + balance < 999999) || (deposit + balance > 0))
      {
        balance = balance + 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 cashwithdraw, chequewithdraw;
      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;
        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)
          printf ("You are limited to a maximum of 500 \n");
        else if (chequewithdraw < 0)
          printf ("Please enter a positive number \n");
        else
        {
          printf ("%.2f has being withdrawn from your account \n", chequewithdraw);
          balance = balance - chequewithdraw;
        }
        break;
      default: 
        printf("Please Enter a Choice from either 1 or 2\n" );
        break;
      }
      
      return (balance);
    }
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Okay, here is the code that i am using completed thus far, this is as up-to-date as i have:

    Code:
    #include <stdio.h>
    
    float deposited (float);
    float withdawn (float);
    
    int main()
    {
    
    int menu_choice = 0;
    float deposit = 0;
    float withdraw = 0;
    int transactions = 0;
    float balance = 0;
    float nodeposits = 0;
    float total_deposited = 0;
    float wibble;
    
    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);
    
    switch (menu_choice)
          {
          case 1:
          printf ("1. Deposit funds \n");
          balance = deposited (balance);
          transactions ++;
          break;
    
          case 2:
          printf ("2. Withdraw funds \n");
          balance = withdrawn (balance, &total_deposited);
          nodeposits ++;
          break;
    
          case 3:
          printf ("3.  Show balance \n");
          printf ("Your current balance = %.2f \n", balance);
          break;
    
          case 4:
          printf ("Exiting... \n");
          printf ("closing balance.............%.2f \n", balance);
          printf ("completed transactions......%.2f \n", transactions);
          printf (" /n");
          printf ("completed deposits..........%.2f \n", nodeposits);
          break;
    
          default: 
          printf("Please Enter a Choice from 1-4\n" );
          break;
          }
    
    }
    
    
    return 0;
    
    }
    
    
    float deposited (float balance)
    { 
      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;
        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 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;
                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;
                }
                break;
    
                default: 
                printf("Please Enter a Choice from either 1 or 2\n" );
                break;
    
                }
        }
    
    
    
    return (balance);
    }
    The bug is:

    Start the program
    Select option 1. deposit funds
    Enter an amount
    Select option 2. withdraw funds
    select option 1. cash
    imput a number

    The screen will display: "your current balance is 45.00" regardless of how much you put in or take out

    please tell me this is just my computer going funny

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >please tell me this is just my computer going funny
    No, it's you ignoring our help. I get three warnings with Dev-C++ and the code you gave in your most recent post, they all had to do with the fact that your prototype doesn't match the definition; a problem we informed you of in every reply since the beginning of this thread I might add. You also call it with one too many arguements.

    There's sound you'll hear often when you ignore people's help, let me give you a sample.

    *plonk*
    My best code is written with the delete key.

  8. #23
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Honest to god, I'm really not ignoring your help, I'm really trying to see what is wrong with my code,

    You are saying there are errors with the prototype and the definition:

    prototype:
    Code:
    float deposited (float);
    float withdrawn (float);
    definition:
    Code:
    float withdrawn (float balance)
    I'm not being ignorant but i just don't see any problems with them unless I'm understanding somthing wrongly. I am honestly not ignoring your help, I just don't understand what you mean.

    I get three warnings with Dev-C++
    I don't get any, i've unticked everything in the C/C++ compiler options tab in the compiler options.

  9. #24
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Check your spelling in the post containing the code:
    >>float withdawn (float);

    Then you call withdrawn like this:
    >>balance = withdrawn (balance, &total_deposited);
    That has two parameters, but the function only takes one.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #25
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Yea, but thats an old piece of code that has being fixed in the new posts, the quotes in my latest post are what is in the code now and it still doesn't want to work

  11. #26
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So paste the latest code, directly from your editor, along with any compiler error/warning messages and a description of what's wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #27
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    just for you:

    Code:
    #include <stdio.h>
    
    float deposited (float);
    float withdawn (float);
    
    int main()
    {
    
    int menu_choice = 0;
    float deposit = 0;
    float withdraw = 0;
    int transactions = 0;
    float balance = 0;
    float nodeposits = 0;
    float total_deposited = 0;
    float wibble;
    
    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);
    
    switch (menu_choice)
          {
          case 1:
          printf ("1. Deposit funds \n");
          balance = deposited (balance);
          transactions ++;
          break;
    
          case 2:
          printf ("2. Withdraw funds \n");
          balance = withdrawn (balance, &total_deposited);
          nodeposits ++;
          break;
    
          case 3:
          printf ("3.  Show balance \n");
          printf ("Your current balance = %.2f \n", balance);
          break;
    
          case 4:
          printf ("Exiting... \n");
          printf ("closing balance.............%.2f \n", balance);
          printf ("completed transactions......%.2f \n", transactions);
          printf (" /n");
          printf ("completed deposits..........%.2f \n", nodeposits);
          break;
    
          default: 
          printf("Please Enter a Choice from 1-4\n" );
          break;
          }
    
    }
    
    
    return 0;
    
    }
    
    
    float deposited (float balance)
    { 
      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;
        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 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;
                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;
                }
                break;
    
                default: 
                printf("Please Enter a Choice from either 1 or 2\n" );
                break;
    
                }
        }
    
    
    
    return (balance);
    }

  13. #28
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That STILL has the spelling mistake in it. It's on the third line of code, you can't miss it

    It also has the other errors I mentioned.

    You didn't even post the compiler messages or give an explanation of what's wrong, like I asked you to. If you want help, you have to play the game..
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #29
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    oh dear god, i honestly cannot belive i missed that!! If someone had just said line 3 of the code i would of found it, thank you, i really feel daft now

    Anyway, i would have assumed you would seen the

    The bug is:

    Start the program
    Select option 1. deposit funds
    Enter an amount
    Select option 2. withdraw funds
    select option 1. cash
    imput a number

    The screen will display: "your current balance is 45.00" regardless of how much you put in or take out
    thing i said above, but with all the code on this page its easy to miss.

    I can't belive how thick i am, you guys must think I'm a right dumbass and i completely agree with you.

    I guess with this sorted then everything is fixed, there is one last thing that i need to do (count the amount deposited) but i should be able to do that on my own.


    i hope.

  15. #30
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by petedee
    Code:
    #include <stdio.h>
    
    float deposited (float balance)
    { 
      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;
        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);
    }
    Lessee here.

    If deposit + balance = 200, then
    if ((200 < 999999) || (200 > 0))
    Both comparisons are true, statement is true

    If deposit + balance = -200, then
    if ((-200 < 999999) || (200 > 0))
    First comparison is true, therefore the statement is true

    If deposit + balance = 2000000, then
    if ((2000000 < 999999) || (2000000 > 0))
    Second comparison is true, therefore the statement is true

    When is it false?

    Originally posted by petedee
    Code:
    float withdrawn (float balance)
    { 
    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;
                printf ("%.2f has being withdrawn from your account \n", cashwithdraw);
    is this print statement the one that claims 45.00?
                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;
                }
                break;
    
                default: 
                printf("Please Enter a Choice from either 1 or 2\n" );
                break;
                }
        }
    return (balance);
    }
    Has anyone mentioned the danger of using scanf()? Search the board for reasons why scanf() is not a good function to use and for alternatives. I'll bet you're not reading the stream correctly.
    Also see this link
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 2 of 3 FirstFirst 123 LastLast
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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21