1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming, where did I get it wrong please

  1. Sep 21, 2016 #1
    Edited by mentor to include image inline
    Hello

    Please I am practicing my simple basic C Programming using Notepad++ and Cygwin64

    I have written tried running the codes in the picture below, it doesn't detect any error when I run it but when I try to 'withdraw some money less Tha the fBalance, it still displays the result insufficient funds, instead of my expected 'You new balance is ... '

    I've tried checking what I missed but couldn't find any. Apparently I am still a newbie in the language.

    Any suggestions please.
    Simple Banking System.JPG
     
    Last edited by a moderator: Sep 21, 2016
  2. jcsd
  3. Sep 21, 2016 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    My suggestion is, post your code here directly w/ "code" tags and don't post it twice.
     
  4. Sep 21, 2016 #3
    Sorry, but w/"code" tags, not clear on that. But I have copied the code directly now. Please be free to comment on any other aspect of the code presented below.

    Mod note: Added code tags to the code below.
    Code (C):

    /*Simple Banking System*/
    # include <stdio.h>
    int main ()
    {
      int iSelection;
      int iTransAmount=0.0;
      float fBalance=100.25;

     printf("\n\tATM\n");
     printf("\n1\tDeposit Funds\n");
     printf("2\tWithdaw Funds\n");
     printf("\nEnter your Selection:");

     scanf("%d",&iSelection);

      if (iSelection==1){
        printf("\nEnter Fund Amount to Deposit:");
        scanf("%f",&iTransAmount);
        printf("\nYour new balance is: $%.2f\n",fBalance + iTransAmount);
      } //end if

      if (iSelection==2){
        printf("Enter Fund Amount to Withdraw:");
        scanf("%f",&iTransAmount);
        if (iTransAmount > fBalance)
        printf("\nInsufficient Funds\n");
      else    
        printf("\nYour new Balance is $%.2f\n",fBalance - iTransAmount);
      } //end if
    } //end main function
     
    Last edited by a moderator: Sep 21, 2016
  5. Sep 21, 2016 #4

    Mark44

    Staff: Mentor

    You have declared iTransAmount as type int, but your calls to scanf() are using the wrong conversion specifier for an int. Both of your variables for money should be float or both double, but not int.

    I see that you are using Hungarian notation (iSelection and fBalance), with "warts" at the beginning indicating the type. I believe that was a fad that came and went some time ago.
     
  6. Sep 21, 2016 #5

    Mark44

    Staff: Mentor

    Here's how the code tags work:
    [code=c] One like this at the top...
    Code (C):
     if (iSelection==2){
         printf("Enter Fund Amount to Withdraw:");
         scanf("%f",&iTransAmount);
         if (iTransAmount > fBalance)
         printf("\nInsufficient Funds\n");
     else
         printf("\nYour new Balance is $%.2f\n",fBalance - iTransAmount);
       } //end if
    [/code] and one like this at the end.
    When you put in these two tags, they won't show up after you click Post Reply - the browser uses them to format the text inside them.
    Comment in addition to those in my previous post -- The else above is indented so that it appears to be paired with the outer if. It isn't. It goes with the if just above it, so should be indented at the same level. The compiler can figure this out, but people reading your code (not "codes") might be thrown.
     
  7. Sep 22, 2016 #6

    rcgldr

    User Avatar
    Homework Helper

    Hungarian notation is still part of the Windows API, so it hangs around. Other "legacy" environments are still using Hungarian notation. On the other hand, C standard library and C++ standard template library do not use Hungarian notation. One somewhat recent convention that I've seen but don't like is for function names to start off with a lower case letter, but then use upper case for the first letter of every word other than the first word in a function, such as mergeSortList().
     
    Last edited: Sep 22, 2016
  8. Sep 22, 2016 #7
    Thank you very much Mark44. The mistake was in the conversion specifier I was using to call my iTransAmount, like you mentioned. I have replaced the %f in the scanf functions with %d and everything works fine now. Really grateful.

    I have also taken note of your comment on the last 'else' indentation, the code tags and on the code (not "codes").

    Thank you :smile::smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C programming, where did I get it wrong please
Loading...