# C programming, where did I get it wrong please

1. Sep 21, 2016

### Simon Clement

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.

Last edited by a moderator: Sep 21, 2016
2. Sep 21, 2016

### phinds

My suggestion is, post your code here directly w/ "code" tags and don't post it twice.

3. Sep 21, 2016

### Simon Clement

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");

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
4. Sep 21, 2016

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

5. Sep 21, 2016

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

6. Sep 22, 2016

### rcgldr

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
7. Sep 22, 2016

### Simon Clement

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