C programming, where did I get it wrong please

AI Thread Summary
The user encountered an issue in their C programming code where withdrawing money resulted in an "Insufficient Funds" message, despite having a sufficient balance. The problem was traced to the incorrect use of the conversion specifier in the `scanf` function for the variable `iTransAmount`, which was declared as an integer but should have been a float. After correcting the specifier from `%f` to `%d`, the code functioned as expected. Additional feedback included suggestions on code formatting and the use of Hungarian notation, which is considered outdated in many contexts. The user expressed gratitude for the assistance and confirmed that the issue was resolved.
Simon Clement
Messages
18
Reaction score
3
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:
Physics news on Phys.org
My suggestion is, post your code here directly w/ "code" tags and don't post it twice.
 
  • Like
Likes 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.
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:
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.
 
  • Like
Likes Simon Clement
Here's how the code tags work:
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.
 
  • Like
Likes Simon Clement
Mark44 said:
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.
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:
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:
 

Similar threads

Back
Top