Still trying to learn functions and I am not sure if I am doing it right. Can someone take a look at my code (Please) I added two functions into it. Did I do them right?
Code:#include <stdio.h> void Print_Intro_Heading(); void print_comment (float amount); int main () { /* Variable Declarations */ /* --------------------- */ char first_name[20]; int num_of_deposits, num_of_withdrawals, i; float amount_to_deposit[50], amount_to_withdraw[50]; float start_balance, current_balance; /*Prompt user to input their name*/ /*-------------------------------*/ printf ("\nWhat is your name? "); scanf ("%s", first_name); fflush(stdin); /* This function prints the users name and a title for the program */ /*-----------------------------------------------------------------*/ { void Print_Intro_Heading(); printf ("\n***********************************************\n" "Hi %s. Welcome to Colleen's Banking program!\n" "***********************************************\n\n", first_name); }// end funtion /* Prompt user for starting balance */ /* -------------------------------- */ do { printf ("Enter current balance in dollars and cents: "); scanf ("%f", &start_balance); if (start_balance < 0) printf("Error: Starting Balance must be at least zero! Please re-enter!\n\n"); } while (start_balance < 0); /* Prompt for the number of withdrawals */ /* -------------------------------------------------------- */ do { printf ("\nEnter the number of withdrawals (0-50): "); scanf ("%i", &num_of_withdrawals); /* Emsure that user did not enter less than 0 withdrawals */ /* Ensure that user did not enter more than 50 withdrawals. */ /*----------------------------------------------------------*/ if (num_of_withdrawals > 50) printf ("*** Maximum amount of withdrawals is 50. Please re-enter!***\n"); if (num_of_withdrawals < 0) printf ("*** Negative number of withdrawals not allowed. ***\n"); } while (num_of_withdrawals > 50 || num_of_withdrawals < 0); /* Prompt for the number of deposits. */ /* ----------------------------------------------------- */ do { printf ("\n\nEnter the number of deposits (0-50): "); scanf ("%i", &num_of_deposits); /* Ensure that user did not enter less than 0 deposits. */ /* Ensure that user did not enter more than 50 deposits. */ /*-------------------------------------------------------*/ if (num_of_deposits > 50) printf ("*** Maximum amount of deposits is 50. Please re-enter! ***\n"); if (num_of_deposits < 0) printf ("*** Negative number of deposits not allowed. ***\n"); } while ( num_of_deposits > 50 || num_of_deposits < 0); /* Retain starting balance for bank record. */ /* ---------------------------------------- */ current_balance = start_balance; /* Prompt for all deposit amounts, keeping track of current balance. */ /* ---------------------------------------------------------------- */ printf ("\n"); // spacing for (i = 0; i < num_of_deposits; i++ ) { do { printf ("Enter the amount of deposit #%i: ", i+1 ); scanf ("%f", &amount_to_deposit[i]); if (amount_to_deposit[i] <= 0) printf ("*** Deposit amount must be positive! Please re-enter ***\n"); } while (amount_to_deposit[i] <= 0); current_balance = current_balance + amount_to_deposit[i]; } // end for loop /* Prompt for all withdrawal amounts, keeping track of current balance. */ /* ------------------------------------------------------------------- */ printf ("\n"); // spacing for (i = 0; i < num_of_withdrawals; i++ ) { /* Enter amounts, and be sure amount does not exceed current balance. */ /* ------------------------------------------------------------------ */ do { printf ("Enter the amount of withdrawal #%i: ", i+1 ); scanf ("%f", &amount_to_withdraw[i]); if (amount_to_withdraw[i] > current_balance) printf ("*** Withdrawal amount exceeds current balance. ***\n"); else if (amount_to_withdraw[i] <= 0) printf ("*** Withdrawal amount must be greater than zero! ***\n"); } while (amount_to_withdraw[i] > current_balance || amount_to_withdraw[i] <= 0); current_balance = current_balance - amount_to_withdraw[i]; /* If Balance goes to zero, no more withdrawals allowed */ if(current_balance == 0) { printf ("\n*** Balance is now zero. No more withdrawals allowed! ***\n"); num_of_withdrawals = i + 1; break; } // end-if } // end for loop. printf ("\n"); // spacing { void print_comment (float amount); /* Output an appropriate closing message based on users balance. */ /* ------------------------------------------------------------- */ printf ("*** The closing balance is $%.2f ***\n", current_balance); if (current_balance >= 50000.00 ) printf ("***%s, Time to invest some money! ***", first_name); else if (current_balance >= 15000.00 ) printf ("***%s, Maybe you should consider a CD. ***", first_name); else if (current_balance >= 1000.00 ) printf ("***%s, Keep up the good work. ***", first_name); else printf ("***%s, Your balance is very low. ***", first_name); } // end function /* Output bank record: Starting balance, deposits, withdrawals and end balance. */ /* -------------------------------------------------------------------------- */ printf ("\n\n"); printf ("*** Bank Record ***\n"); printf ("Starting Balance: $%13.2f\n\n", start_balance); for (i = 0; i < num_of_deposits; i++ ) printf ("Deposit #%2i: %15.2f\n", i+1, amount_to_deposit[i]); if (num_of_deposits > 0) printf ("\n"); // spacing for (i = 0; i < num_of_withdrawals; ++i ) printf ("Withdrawal #%2i: %15.2f\n", i+1, amount_to_withdraw[i]); printf ("\nEnding Balance: $%15.2f\n\n", current_balance); } // end main.



LinkBack URL
About LinkBacks



I am so lost. Will I ever get this?