Hi All,
I have written a program that doesn't seem to work and I can't figure out why it's not working. It compiles correctly, but when I run it I get prompted by the first couple of printf statements, as it should, but then the rest of the code doesn't run. Could someone please help me?
Thanks for your help! -
-
#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 Milas Banking System.\n\n");
-
-
printf("Please enter your first name: ");
-
scanf("%s", first_name);
-
fflush(stdin);
-
-
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);
-
fflush(stdin);
-
-
if (start_bal < 0)
-
printf("Invalid entry. Starting balance must be at least zero!\n\n");
-
-
} while (start_bal > 0); /*END DO WHILE*/
-
-
return start_bal;
-
-
/* end function get starting balance */
-
-
/*START FUNCTION GET NUMBER OF WITHDRAWLS*/
-
-
do{
-
-
printf ("\nEnter the number of withdrawals: ");
-
scanf ("%i",&num_withdrawals);
-
fflush (stdin);
-
-
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 */
-
-
return num_withdrawals;
-
-
/* end function number of withdrawls */
-
-
/*START FUNCTION GET NUMBER OF DEPOSITS*/
-
-
do{
-
-
printf ("\nEnter the number of deposits: ");
-
scanf ("%i",&num_deposits);
-
fflush (stdin);
-
-
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 */
-
-
return num_deposits;
-
-
/* end function number of deposits */
-
-
/*START FUNCTION GET EACH DEPOSIT*/
-
-
for (x = 1; x <= num_deposits; x++)
-
-
do{
-
-
printf ("Enter the amount of deposit #%i: ", x);
-
scanf ("%f",&deposits[x]);
-
fflush (stdin);
-
-
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];
-
-
/* end for loop */
-
/* end function get each deposit */
-
-
/* START FUNCTION GET EACH WITHDRAWL */
-
-
for (x = 1; x <= num_withdrawals; x++)
-
-
do{
-
-
printf ("Enter the amount of withdrawal #%i: ", x);
-
scanf ("%f",&withdrawals[x]);
-
fflush (stdin);
-
-
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 function get each withdrawl*/
-
-
/*START FUNCTION 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;
-
-
/* break; */
-
/*end-if*/
-
} /*end for loop*/
-
/*end function check balance*/
-
/*START FUNCTION CALC 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*/
-
-
/*end function calc and display balance*/
-
-
/*START FUNCTION DISPLAY BANK RECORD*/
-
-
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);
-
-
}
-
-
3 1674
Your main() function basically looks like this: -
int main() {
-
/* ask for correct input */
-
return correct_input;
-
/* ask for more correct input */
-
return more_correct_input;
-
/* etc. etc. */
-
}
-
Why are you returning from your main() function when it has more work to do?
kind regards,
Jos
Hi Jos,
Thanks for your reply.
In answer to your question, I'm not sure why I'm returning from my main function. I'm actually quite confused as to what I'm doing wrong.
any help would be appreciated
Thanks
Hi Jos,
Thanks for your reply.
In answer to your question, I'm not sure why I'm returning from my main function. I'm actually quite confused as to what I'm doing wrong.
any help would be appreciated
Thanks
If you return from your main() function it returns to its calling function. Most of the
time it will return to the exit() function which terminates the entire process.
Design little functions that return the data from the user, e.g. -
float get_start_balance() {
-
-
float start_bal;
-
-
do {
-
/* Prompt user for current balance. */
-
printf("%s, Please enter your current balance in dollars and cents: ");
-
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*/
-
-
return start_bal;
-
}
-
Note that I simply stole your code and made a little function out of it. In your main()
function simply call that function: -
int main() {
-
float start_bal;
-
...
-
start_bal= get_start_bal();
-
...
-
/* the rest of your code here */
-
}
-
Repeat the same scenario for your other values and don't return from main() until
you have completed everything.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: clinttoris |
last post by:
Hello,
If someone could help me it would be appreciated as I am not having
much luck.
I'm struggling with my asp code and have some questions relating to asp
and oracle database.
First...
|
by: Jay |
last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send
text messages to many, many employees via system.timer at a 5 second
interval. Basically, I look in a SQL table (queue) to...
|
by: asif929 |
last post by:
I need immediate help in writing a function program. I have to write a
program in functions and use array to store them. I am not familiar
with functions and i tried to create it but i fails to...
|
by: gunimpi |
last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431
********************************************************
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
wanted...
|
by: darrel |
last post by:
Hi vb master i need help on this
here my code:
If Combo1(0) = rs.Fields("TimeStart") And Combo4(0) = rs.Fields("TimeEnd") And Combo2(0) = rs.Fields("ROOM") And Combo3(0) = rs.Fields("DAYS")...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |