C programming, where did I get it wrong please

In summary: I understand now.In summary, the programmer was having trouble with their code and needed help. They had declared their variables as type int, but their calls to scanf() were using the wrong conversion specifier for an int. They had also made a mistake in their code.
  • #1
Simon Clement
18
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
  • #2
My suggestion is, post your code here directly w/ "code" tags and don't post it twice.
 
  • Like
Likes Simon Clement
  • #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.
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:
  • #4
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
  • #5
Here's how the code tags work:
[code=c] One like this at the top...
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
  • #6
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:
  • #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:
 

1. What is C programming?

C programming is a high-level computer programming language that was developed in the 1970s. It is still widely used today for developing operating systems, system software, and embedded systems. It is known for its efficiency, flexibility, and low-level access to memory.

2. How do I start learning C programming?

To start learning C programming, it is recommended to have a basic understanding of computer programming principles and familiarity with another programming language. There are many online resources and books available for learning C, and it is helpful to practice writing and debugging code on a regular basis.

3. What are some common mistakes made in C programming?

Some common mistakes in C programming include forgetting to include necessary libraries, using uninitialized variables, and not properly allocating and freeing memory. It is important to carefully review and test your code to catch any errors before running it.

4. How can I improve my C programming skills?

To improve your C programming skills, it is helpful to practice regularly, read and understand code written by others, and participate in online communities or programming forums to ask questions and learn from others. It is also beneficial to continuously seek out new challenges and projects to work on.

5. What are some applications of C programming?

C programming is commonly used for developing operating systems, device drivers, system software, and embedded systems. It is also used in various applications such as game development, database management, and scientific computing. Its efficiency and low-level access to memory make it a popular choice for projects that require high performance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • STEM Academic Advising
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Science and Math Textbooks
Replies
16
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
866
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
40
Views
2K
Back
Top