Thread: help with storing the last value of a loop and adding it?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    help with storing the last value of a loop and adding it?

    I am currently working on a homework assignment I am having a hard time adding the last value from a while loop :


    Code:
     while (!feof(fp)) {
    
              fscanf(fp, "%c %f\n", &code, &amount);
              
    		  if(code == 'I') {
    
                 printf("Initial Balance                                                    %.2f\n",amount);
    			 intitial_balance = amount;
               }
    		  if (code == 'D')
    		  {
                
    			
                
    
                
                deposit = amount + intitial_balance;
                printf("Deposit                $%.2f                                   $%.2f\n",amount,deposit);
    
    			intitial_balance = deposit;
    
                
    
    
    
    			depositcounter++;
    			
    			
    
    
    
                
                			 
                
    		  }
    
    
    		   
    		  
    
              
    		  if (code == 'C')
    		  {
    
               cashcheck = intitial_balance - amount;
    		   printf("Check Cash                                  $%.2f             $%.2f\n",amount,cashcheck);
    
    		   intitial_balance = cashcheck;
    
               if (intitial_balance < 0.00 )
    		   {
    			   overdraft++;
    
    		   }
    
    
    
    		  }
    
    	 }
    printf(" There were %d Deposits width the Amount of %4.2f with the deposits\n",depositcounter,deposit_oa1);


    I have everything I need but there is just one thing that I can't figure out and it's how to get the over all number in deposit and checks cashed . Meaning adding all the deposits , like if my first deposit was 144.35 then the next one is 166.88 I have to add them.

    I hope this makes sense .....

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Did you actually preview your post before sending it? It looks like you have used some esoteric language between the code. Anyway don't use feof it doesn't work the way you want.
    Sample:
    Code:
    char line[1024];
    
    while( fgets(line, 1024, fp) != NULL ) {
        sscanf(line, "&#37;c %f\n", &code, &amount);
        ...
    }
    The above handles EOF gravefully.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would add
    Code:
    while( fgets(line, 1024, fp) != NULL ) {
        if(sscanf(line, "&#37;c %f\n", &code, &amount) != 2)
        {
            /* incorrect line format - skipping */
            continue;
        }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    how stupid of me I accidently erased something front the top sorry guys


    the assignment I am doing is a checking account which gets the inputs from an external source ( and txt file) the txt file has a starting character which Id's what type of transaction
    ( I = initial Balance , D = Deposit , C = Checks cashed) . and then is has the number either depositing or taking away.

    Example of txt file :

    I 498.98
    D 866.79
    C 289.98


    what the professor wants us to do is to make a monthly statement and produce a list of the transactions and the balance of the account


    Transaction Deposits Checks Cashed Balance
    Initial Balance 498.98
    Deposit 1.00 1.00
    Check Cashed 10.00 488.98




    also wants us to output the how many deposits and check cashed , also to total how much money was deposited and taking away.

    Where I am having trouble is in adding all the values from Deposit and check cashed the while loops the code and the amount. I got the initial balance taking care of but just adding up all the values in deposit and checks cashed to get the amount


    I hope this helps

    and here is my source code


    Code:
    #include <stdio.h>
    
    
     FILE *fp;
     int main(void)
     {
         int depositcounter =0;
         float deposit;
    	 float intitial_balance;
    	 float temp_deposit[30];
    	 int overdraft = 0;
    	 float deposit_overall;
    	 int deposit_trigger=0;
    	 float deposit_oa1;
    	 int i;
    	 float deposit_oa2;
    	 float cashcheck;
         char code;
    	 float amount;
    	 fopen_s(&fp, "account.txt", "r");
         
         printf("Transaction             Deposit             Cashed Checks         Balance\n\n");
    	 
    	 
    
    
    	 while (!feof(fp)) {
    
              fscanf(fp, "%c %f\n", &code, &amount);
              
    		  if(code == 'I') {
    
                 printf("Initial Balance                                                    %.2f\n",amount);
    			 intitial_balance = amount;
               }
    		  if (code == 'D')
    		  {
                
    			  
    
    
                deposit = amount + intitial_balance;
                printf("Deposit                $%.2f                                   $%.2f\n",amount,deposit);
    
    			intitial_balance = deposit;
    
    
                depositcounter++;
    
                if (deposit_trigger == 0)
    			{
    			
                
    
    
    
    		
    			
    			
    
    
    
                
                			 
                
    		  }
    
    
    		   
    		  
    
              
    		  if (code == 'C')
    		  {
    
               cashcheck = intitial_balance - amount;
    		   printf("Check Cash                                  $%.2f             $%.2f\n",amount,cashcheck);
    
    		   intitial_balance = cashcheck;
    
               if (intitial_balance < 0.00 )
    		   {
    			   overdraft++;
    
    		   }
    
    
    
    		  }
    
    	 }
    printf(" There were %d Deposits width the Amount of %4.2f with the deposits\n",depositcounter,deposit_oa1);
    
    printf("There Are %d Overdraft\n",overdraft);
    
    	 return 0;
    
     }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use foef to control loop - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding an array using a For loop
    By C++Noob316 in forum C++ Programming
    Replies: 11
    Last Post: 03-12-2009, 01:13 PM
  2. flag-controlled while loop
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2008, 02:48 PM
  3. Adding output of a FOR loop
    By cjohnson412 in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2008, 07:41 PM
  4. Two cins in a loop
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2004, 02:08 AM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM