C programming, where did I get it wrong please

Click For Summary

Discussion Overview

The discussion revolves around a problem encountered in a simple C programming project related to a banking system. The participant is seeking help with code that does not produce the expected output when attempting to withdraw funds, despite no errors being detected during execution. The scope includes programming practices, debugging, and code formatting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The initial participant describes an issue where the program incorrectly displays "Insufficient Funds" when attempting to withdraw an amount less than the balance.
  • Another participant suggests posting the code directly using "code" tags for clarity.
  • It is noted that the variable iTransAmount is declared as an int, but the scanf calls use the wrong conversion specifier, indicating a potential source of error.
  • There is a discussion about Hungarian notation, with some participants commenting on its relevance and usage in modern programming practices.
  • A participant acknowledges the mistake regarding the conversion specifier and reports that correcting it resolves the issue.
  • Comments are made about code formatting, particularly regarding the indentation of the else statement in relation to the if statement.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to the conversion specifier and code formatting. However, there is no consensus on the relevance of Hungarian notation, as opinions vary on its usage in contemporary programming.

Contextual Notes

The discussion highlights limitations in variable type declarations and conversion specifiers, as well as the potential for confusion in code readability due to formatting choices. These aspects remain unresolved in terms of best practices.

Who May Find This Useful

Individuals learning C programming, particularly those interested in debugging practices and code formatting conventions.

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   Reactions: 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   Reactions: 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   Reactions: 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

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K