Help with C code

Reply

Join Date: Oct 2008
Posts: 2
Reputation: gmdune is an unknown quantity at this point 
Solved Threads: 0
gmdune gmdune is offline Offline
Newbie Poster

Help with C code

 
0
  #1
Oct 23rd, 2008
Hi All,

Thanks for all your help and suggestions...I am re-posting my code, with proper formatting for your review. The code runs correctly, but the ending balance, after all the withdrawals, is incorrect and I can't find where I messed up. Any help would be most appreciated:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. /* Declare Array Variables */
  7.  
  8. float deposits[50] = {0};
  9. float withdrawals[50] = {0};
  10. char first_name[20] = {0};
  11.  
  12. /* Declare Variables */
  13.  
  14. int num_withdrawals, num_deposits, x;
  15. float current_balance = 0;
  16. float start_bal;
  17. float total_deposits;
  18. float total_withdrawals;
  19. float balance;
  20.  
  21. /* Output initial greeting */
  22.  
  23. printf("Welcome to the Banking System.\n\n");
  24.  
  25. printf("Please enter your first name: ");
  26. scanf("%s", first_name);
  27.  
  28. printf("\nHello, %s.\n\n", first_name);
  29.  
  30. /* Get Starting Balance */
  31.  
  32. do {
  33.  
  34. printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
  35. scanf("%f", &start_bal);
  36.  
  37. if (start_bal < 0)
  38. printf("Invalid entry. Starting balance must be at least zero!\n\n");
  39.  
  40. } while (start_bal < 0); /* End do-while */
  41.  
  42. /* end function get starting balance */
  43.  
  44. /* Get Number of Withdrawals */
  45.  
  46. do {
  47.  
  48. printf ("\nEnter the number of withdrawals: ");
  49. scanf ("%i", &num_withdrawals);
  50. printf ("\n");
  51.  
  52. if (num_withdrawals < 0 || num_withdrawals > 50)
  53. printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
  54.  
  55. } while (num_withdrawals < 0 || num_withdrawals > 50); /* end do-while trap loop */
  56.  
  57. /* end function number of withdrawls */
  58.  
  59. /* Get Number of Deposits */
  60.  
  61. do {
  62.  
  63. printf ("Enter the number of deposits: ");
  64. scanf ("%i",&num_deposits);
  65. printf ("\n");
  66.  
  67. if ( num_deposits < 0 || num_deposits > 50)
  68. printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
  69.  
  70. } while (num_deposits < 0 || num_deposits > 50); /* end do-while trap loop */
  71.  
  72. /* Get Each Deposit */
  73.  
  74. for (x = 1; x <= num_deposits; x++)
  75.  
  76. do {
  77.  
  78. printf ("Enter the amount of deposit #%i: ", x);
  79. scanf ("%f",&deposits[x]);
  80.  
  81. if (deposits[x] <= 0)
  82. printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
  83.  
  84. } while (deposits[x] <= 0);
  85.  
  86. total_deposits = total_deposits + deposits[x];
  87. current_balance = total_deposits + start_bal;
  88.  
  89. /* end for loop */
  90.  
  91. /* Get Each Withdrawal */
  92.  
  93. for (x = 1; x <= num_withdrawals; x++)
  94.  
  95. do {
  96.  
  97. printf ("\n");
  98. printf ("Enter the amount of withdrawal #%i: ", x);
  99. scanf ("%f",&withdrawals[x]);
  100.  
  101. if (withdrawals[x] > current_balance)
  102. printf ("***Withdrawal amount exceeds current balance.***\n");
  103.  
  104. else
  105. if (withdrawals[x] <= 0)
  106. printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
  107.  
  108. } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
  109.  
  110. total_withdrawals = total_withdrawals + withdrawals[x];
  111.  
  112. /* end get each withdrawl */
  113.  
  114. /* Check Balance */
  115.  
  116. balance = current_balance - total_withdrawals + total_deposits;
  117.  
  118. if (balance == 0)
  119.  
  120. {
  121.  
  122. printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
  123.  
  124. num_withdrawals = x;
  125.  
  126. }
  127.  
  128.  
  129. /* Calculate and Display Balance */
  130.  
  131. {
  132.  
  133. printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
  134. if (balance >= 5000.00)
  135.  
  136. printf ("*** Time to invest some money!*** \n\n");
  137. else if (balance >= 15000.00 && balance <= 49999.99)
  138.  
  139. printf ("*** Maybe you should consider a CD.*** \n\n");
  140. else if (balance >= 1000.00 && balance <= 14999.99)
  141.  
  142. printf ("*** Keep up the good work.*** \n\n");
  143.  
  144. else
  145. printf ("*** %s, your balance is getting low!*** \n\n", first_name);
  146.  
  147. } /*end-if*/
  148.  
  149. /* Calculate and display balance*/
  150.  
  151. printf (" *** Bank Record ***\n");
  152.  
  153. printf ("\nStarting Balance: $%13.2f\n\n", current_balance);
  154.  
  155. for (x = 1; x <= num_deposits; x++)
  156.  
  157. {
  158.  
  159. printf ("Deposit #%i: %13.2f\n", x, deposits[x]);
  160.  
  161. }
  162.  
  163. for (x = 1; x <= num_withdrawals; x++)
  164.  
  165. {
  166.  
  167. printf ("\nWithdrawal #%i: %13.2f", x, withdrawals[x]);
  168.  
  169. }
  170.  
  171. printf ("\n\nEnding Balance is: $%13.2f\n", balance);
  172.  
  173. /* end function display bank record */
  174.  
  175. getchar(); /* Pause output */
  176.  
  177. return (0);
  178.  
  179. } /* end main */
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with C code

 
0
  #2
Oct 23rd, 2008
>>.I am re-posting my code
This is your first post -- so how can that be ???

>>The code runs correctly, but the ending balance, after all the withdrawals, is incorrect

Then it doesn't run correctly, now does it??


What compiler are you using? VC++ 2008 Express produces a couple warnings that are really errors. You need to correct them.

  1. 1>c:\dvlp\test1\test1\test1.cpp(93) : warning C4700: uninitialized local variable 'total_deposits' used
  2. 1>c:\dvlp\test1\test1\test1.cpp(117) : warning C4700: uninitialized local variable 'total_withdrawals' used
Last edited by Ancient Dragon; Oct 23rd, 2008 at 11:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC