473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help With C code looping

4
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!

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.  
  7.     /* Declare Array Variables */
  8.  
  9.     float deposits[50] = {0}; 
  10.     float withdrawals[50] = {0};
  11.     char  first_name[20] = {0};
  12.  
  13.     /* Declare Variables */
  14.  
  15.     int   num_withdrawals, num_deposits, x;
  16.     float current_balance = {0};
  17.     float start_bal;
  18.     float total_deposits;
  19.     float total_withdrawals;
  20.     float balance;
  21.  
  22.     /* Output initial greeting */
  23.  
  24.     printf("Welcome to the Milas Banking System.\n\n");
  25.  
  26.     printf("Please enter your first name: ");
  27.     scanf("%s", first_name);
  28.     fflush(stdin);
  29.  
  30.     printf("\nHello, %s.\n\n", first_name);
  31.  
  32.     /* Get Starting Balance */    
  33.  
  34.     do{
  35.  
  36.     printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
  37.     scanf("%f", &start_bal);
  38.     fflush(stdin);
  39.  
  40.     if (start_bal < 0)
  41.     printf("Invalid entry. Starting balance must be at least zero!\n\n");
  42.  
  43.     } while (start_bal > 0); /*END DO WHILE*/
  44.  
  45.     return start_bal;
  46.  
  47.     /* end function get starting balance */
  48.  
  49.     /*START FUNCTION GET NUMBER OF WITHDRAWLS*/
  50.  
  51.     do{
  52.  
  53.     printf ("\nEnter the number of withdrawals: ");
  54.     scanf ("%i",&num_withdrawals);
  55.     fflush (stdin);
  56.  
  57.     if (num_withdrawals < 0 || num_withdrawals > 50)
  58.     printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
  59.  
  60.     } while (num_withdrawals < 0 || num_withdrawals > 50); /* end do-while trap loop */
  61.  
  62.     return num_withdrawals;
  63.  
  64.     /* end function number of withdrawls */
  65.  
  66.     /*START FUNCTION GET NUMBER OF DEPOSITS*/
  67.  
  68.     do{
  69.  
  70.     printf ("\nEnter the number of deposits: ");
  71.     scanf ("%i",&num_deposits);
  72.     fflush (stdin);
  73.  
  74.     if ( num_deposits < 0 || num_deposits > 50)
  75.     printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
  76.  
  77.     } while (num_deposits < 0 || num_deposits > 50); /* end do-while trap loop */
  78.  
  79.     return num_deposits;
  80.  
  81.     /* end function number of deposits */
  82.  
  83.     /*START FUNCTION GET EACH DEPOSIT*/
  84.  
  85.     for (x = 1; x <= num_deposits; x++)
  86.  
  87.     do{
  88.  
  89.     printf ("Enter the amount of deposit #%i: ", x);
  90.     scanf ("%f",&deposits[x]);
  91.     fflush (stdin);
  92.  
  93.     if (deposits[x] <= 0)
  94.     printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
  95.  
  96.     } while (deposits[x] <= 0);
  97.  
  98.     total_deposits = total_deposits + deposits[x];
  99.  
  100.     /* end for loop */
  101.     /* end function get each deposit */
  102.  
  103.     /* START FUNCTION GET EACH WITHDRAWL */
  104.  
  105.     for (x = 1; x <= num_withdrawals; x++)
  106.  
  107.     do{
  108.  
  109.     printf ("Enter the amount of withdrawal #%i: ", x);
  110.     scanf ("%f",&withdrawals[x]);
  111.     fflush (stdin);
  112.  
  113.     if (withdrawals[x] > current_balance)
  114.  
  115.     printf ("***Withdrawal amount exceeds current balance.***\n");
  116.  
  117.         else
  118.         if (withdrawals[x] <= 0)
  119.  
  120.     printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
  121.  
  122.     } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
  123.  
  124.     total_withdrawals = total_withdrawals + withdrawals[x];
  125.  
  126.     /*end function get each withdrawl*/
  127.  
  128.     /*START FUNCTION CHECK BALANCE*/
  129.  
  130.     balance = current_balance - total_withdrawals + total_deposits;
  131.  
  132.     if (balance == 0)
  133.  
  134.     {
  135.  
  136.     printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
  137.  
  138.     num_withdrawals = x;
  139.  
  140.     /*   break; */
  141.     /*end-if*/
  142.     } /*end for loop*/
  143.     /*end function check balance*/
  144.     /*START FUNCTION CALC AND DISPLAY BALANCE*/
  145.  
  146.     {
  147.  
  148.     printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
  149.     if (balance >= 5000.00)
  150.  
  151.     printf ("*** Time to invest some money!*** \n\n");
  152.     else if (balance >= 15000.00 && balance <= 49999.99)
  153.  
  154.     printf ("*** Maybe you should consider a CD.*** \n\n");
  155.     else if (balance >= 1000.00 && balance <= 14999.99)
  156.  
  157.     printf ("*** Keep up the good work.*** \n\n");
  158.  
  159.     else
  160.     printf ("*** %s, your balance is getting low!*** \n\n", first_name);
  161.  
  162.     }   /*end-if*/
  163.  
  164.     /*end function calc and display balance*/
  165.  
  166.     /*START FUNCTION DISPLAY BANK RECORD*/
  167.  
  168.     printf ("     *** Bank Record ***\n");
  169.  
  170.     printf ("\nStarting Balance:$   %13.2f\n\n", current_balance);
  171.  
  172.     for (x = 1; x <= num_deposits; x++)
  173.  
  174.     {
  175.  
  176.     printf ("Deposit #%i:          %13.2f\n", x, deposits[x]);
  177.  
  178.     }
  179.  
  180.     for (x = 1; x <= num_withdrawals; x++)
  181.  
  182.     {
  183.  
  184.     printf ("\nWithdrawal  #%i:      %13.2f", x, withdrawals[x]);
  185.  
  186.     }
  187.     printf ("\n\nEnding Balance is:$  %13.2f\n", balance);
  188.  
  189.     /*end function display bank record*/
  190.  
  191.     getchar(); /* Pause output */
  192.  
  193.     return (0);
  194.  
  195. }
  196.  
  197.  
Oct 23 '08 #1
3 1674
JosAH
11,448 Expert 8TB
Your main() function basically looks like this:

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.    /* ask for correct input */
  3.    return correct_input;
  4.    /* ask for more correct input */
  5.    return more_correct_input;
  6.    /* etc. etc. */
  7. }
  8.  
Why are you returning from your main() function when it has more work to do?

kind regards,

Jos
Oct 23 '08 #2
gmdune
4
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
Oct 23 '08 #3
JosAH
11,448 Expert 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. float get_start_balance() {
  2.  
  3.    float start_bal;
  4.  
  5.    do {
  6.       /* Prompt user for current balance. */ 
  7.       printf("%s, Please enter your current balance in dollars and cents: "); 
  8.        scanf("%f", &start_bal); 
  9.  
  10.       if (start_bal < 0) 
  11.         printf("Invalid entry. Starting balance must be at least zero!\n\n"); 
  12.  
  13.    } while (start_bal < 0); /*END DO WHILE*/ 
  14.  
  15.    return start_bal; 
  16. }
  17.  
Note that I simply stole your code and made a little function out of it. In your main()
function simply call that function:

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.    float start_bal;
  3.    ...
  4.    start_bal= get_start_bal();
  5.    ...
  6.    /* the rest of your code here */
  7. }
  8.  
Repeat the same scenario for your other values and don't return from main() until
you have completed everything.

kind regards,

Jos
Oct 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
15
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...
21
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...
0
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...
1
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")...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.