| | |
Help with C code
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { /* Declare Array Variables */ float deposits[50] = {0}; float withdrawals[50] = {0}; char first_name[20] = {0}; /* Declare Variables */ int num_withdrawals, num_deposits, x; float current_balance = 0; float start_bal; float total_deposits; float total_withdrawals; float balance; /* Output initial greeting */ printf("Welcome to the Banking System.\n\n"); printf("Please enter your first name: "); scanf("%s", first_name); printf("\nHello, %s.\n\n", first_name); /* Get Starting Balance */ do { printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */ scanf("%f", &start_bal); if (start_bal < 0) printf("Invalid entry. Starting balance must be at least zero!\n\n"); } while (start_bal < 0); /* End do-while */ /* end function get starting balance */ /* Get Number of Withdrawals */ do { printf ("\nEnter the number of withdrawals: "); scanf ("%i", &num_withdrawals); printf ("\n"); if (num_withdrawals < 0 || num_withdrawals > 50) printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n"); } while (num_withdrawals < 0 || num_withdrawals > 50); /* end do-while trap loop */ /* end function number of withdrawls */ /* Get Number of Deposits */ do { printf ("Enter the number of deposits: "); scanf ("%i",&num_deposits); printf ("\n"); if ( num_deposits < 0 || num_deposits > 50) printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n"); } while (num_deposits < 0 || num_deposits > 50); /* end do-while trap loop */ /* Get Each Deposit */ for (x = 1; x <= num_deposits; x++) do { printf ("Enter the amount of deposit #%i: ", x); scanf ("%f",&deposits[x]); if (deposits[x] <= 0) printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n"); } while (deposits[x] <= 0); total_deposits = total_deposits + deposits[x]; current_balance = total_deposits + start_bal; /* end for loop */ /* Get Each Withdrawal */ for (x = 1; x <= num_withdrawals; x++) do { printf ("\n"); printf ("Enter the amount of withdrawal #%i: ", x); scanf ("%f",&withdrawals[x]); if (withdrawals[x] > current_balance) printf ("***Withdrawal amount exceeds current balance.***\n"); else if (withdrawals[x] <= 0) printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***"); } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */ total_withdrawals = total_withdrawals + withdrawals[x]; /* end get each withdrawl */ /* Check Balance */ balance = current_balance - total_withdrawals + total_deposits; if (balance == 0) { printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n"); num_withdrawals = x; } /* Calculate and Display Balance */ { printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name); if (balance >= 5000.00) printf ("*** Time to invest some money!*** \n\n"); else if (balance >= 15000.00 && balance <= 49999.99) printf ("*** Maybe you should consider a CD.*** \n\n"); else if (balance >= 1000.00 && balance <= 14999.99) printf ("*** Keep up the good work.*** \n\n"); else printf ("*** %s, your balance is getting low!*** \n\n", first_name); } /*end-if*/ /* Calculate and display balance*/ printf (" *** Bank Record ***\n"); printf ("\nStarting Balance: $%13.2f\n\n", current_balance); for (x = 1; x <= num_deposits; x++) { printf ("Deposit #%i: %13.2f\n", x, deposits[x]); } for (x = 1; x <= num_withdrawals; x++) { printf ("\nWithdrawal #%i: %13.2f", x, withdrawals[x]); } printf ("\n\nEnding Balance is: $%13.2f\n", balance); /* end function display bank record */ getchar(); /* Pause output */ return (0); } /* end main */
>>.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.
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.
C Syntax (Toggle Plain Text)
1>c:\dvlp\test1\test1\test1.cpp(93) : warning C4700: uninitialized local variable 'total_deposits' used 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.
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






