Thread: Problems with code

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    Problems with code

    Hey! im written a program to look at some banking transactions they are below:
    Code:
    541.43
    02/04/2006 -50.00
    03/04/2006 -475.00
    06/04/2006 -20.00
    06/04/2006 -10.00
     8/04/2006 +140.50
    12/04/2006 -20.00
    15/04/2006 -10.00
    20/04/2006 -83.27
    20/04/2006 +50.00
    23/04/2006 -30.00
    25/04/2006 -15.00
    29/04/2006 +50.00
    and here is the code i have so far:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    main()
    {
    FILE *openmr_Smith;                       
    float initialBalance;
    int n;
    char fileName[50];
    float annualInterestRate;
    int i;
    float currentBalance;
    float dailyInterestRate;
    float dailyInterest;
    float totalInterest;
    
    
    struct transactions {
                        int day;
                        int month;
                        int year;
                        float transaction;
                        };
    struct transactions Data[100];
    
    printf("Please enter the filename: ");
    scanf("%s",&fileName);
    
    openmr_Smith=fopen((fileName),"r");  
    if (openmr_Smith==NULL) printf("This file name is not recognised");   
    else
    {
    	
    	printf("Please enter the annual interest rate: "); 
    	scanf("%f",&annualInterestRate);
    	printf("You entered: %4.2f %\n",annualInterestRate);
    	getchar();
    
    	printf("Opening file: %s ...\n",fileName);
    	fscanf(openmr_Smith, "%f", &initialBalance);
    	
    	for (n=0;n<100;n++)
    	{
    		fscanf(openmr_Smith, "%i/%i/%i %f", &Data[n].day, &Data[n].month, &Data[n].year, &Data[n].transaction);
    	}
    	fclose(openmr_Smith);
    
    	currentBalance=initialBalance;
    	totalInterest=0;
    	
    
    	for (i=1;i<31;i++)
    	{
    		for (n=0;n<100;n++)
    		{
    		if(i==Data[n].day)
    		{
    		currentBalance=currentBalance+Data[n].transaction;
    		
    		if(Data[n].transaction<0)
    		if(currentBalance<0)
    		{
    		currentBalance=currentBalance-10;
    		}
    		}
    		if(currentBalance>0)
    		{
    		dailyInterestRate=((log((annualInterestRate/100)+1))/3.65);
    		dailyInterest=((currentBalance/100)*dailyInterestRate);
    		totalInterest=totalInterest+dailyInterest;
    		}
    		
    		}
    		
    		printf("%4.2f\n",currentBalance);
    		printf("%f\n",totalInterest);
    	}
    
    
    printf("%f\n",totalInterest);
    }
    return 0;
    }
    The problem that i am faced with now is that i am unable to get the total interest by using the code above to get it all of the numbers change and it will not work using "totalInterest=totalInterest+dailyInterest" which i cannot understand because i used the same method for currentBalance and it worked fine.

    If anyone could offer any help to get the total interest (which is the running total of the interest gained from the currentBalance being positive at the end of the day) it would be very helpful. x x

    Another quick thing is there anyway of being able to read in the day "08" because for it to work i have had to change it to "8".

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Change the &#37;i's inside your fscanf() to %d's. %i will treat all numbers starting with 0x as hexadecimal and 0 as octal. %d will not.

    I can't see offhand what the problem is with calculating the interest. Make sure that dailyInterest is being assigned the proper value in every iteration of the loop....

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Thank you ive changed it to %d and that bit works fine now!

    This bit works fine and gives the corect dailyinterest after each day:
    Code:
    dailyInterest=((currentBalance/100)*dailyInterestRate);
    its just as soon as i type in:
    Code:
    totalInterest=totalInterest+dailyInterest;
    it does the first days interest correctly and then it goes wrong it just prints out the same huge number repeatly for both the currentBalance and totalInterest would there be any reason for this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some comments marked by !!
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {                               /* be specific about return types - implicit declarations are old */
        FILE *openmr_Smith;
        float initialBalance;
        int n;
        char fileName[50];
        float annualInterestRate;
        int i;
        float currentBalance;
        float dailyInterestRate;
        float dailyInterest;
        float totalInterest;
    
        struct transactions {
            int day;
            int month;
            int year;
            float transaction;
        };
        struct transactions Data[100];
    
        printf("Please enter the filename: ");
        /*!! foo.c:28: warning: char format, different type arg (arg 2) */
        /*!! should be just fileName, not &fileName */
        scanf("&#37;s", &fileName);
    
        openmr_Smith = fopen((fileName), "r");
    
        /*!! use {} even for single statement - it will save confusion eventually. */
        if (openmr_Smith == NULL) {     
            printf("This file name is not recognised");
        } else {
            printf("Please enter the annual interest rate: ");
            scanf("%f", &annualInterestRate);
            /*!! foo.c:40: warning: unknown conversion type character 0xa in format */
            /*!! To print a single %, write %% */
            printf("You entered: %4.2f %\n", annualInterestRate);
            getchar();
    
            printf("Opening file: %s ...\n", fileName);
            fscanf(openmr_Smith, "%f", &initialBalance);
    
            for (n = 0; n < 100; n++) {
                fscanf(openmr_Smith, "%i/%i/%i %f", &Data[n].day,
                       &Data[n].month, &Data[n].year, &Data[n].transaction);
            }
            fclose(openmr_Smith);
    
            currentBalance = initialBalance;
            totalInterest = 0;
    
            for (i = 1; i < 31; i++) {
                for (n = 0; n < 100; n++) {
                    if (i == Data[n].day) {
                        currentBalance = currentBalance + Data[n].transaction;
    
                        if (Data[n].transaction < 0)
                            if (currentBalance < 0) {
                                currentBalance = currentBalance - 10;
                            }
                    }
                    if (currentBalance > 0) {
                        dailyInterestRate =
                            ((log((annualInterestRate / 100) + 1)) / 3.65);
                        dailyInterest =
                            ((currentBalance / 100) * dailyInterestRate);
                        totalInterest = totalInterest + dailyInterest;
                    }
                }
                printf("%4.2f\n", currentBalance);
                printf("%f\n", totalInterest);
            }
            printf("%f\n", totalInterest);
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Hey! thank you very much for your help ive made the changes now but im not really sure wat is meant by these warnings? and is this the reason why i am unable to calcukate the totalinterest correctly?

    /*!! foo.c:28: warning: char format, different type arg (arg 2) */
    /*!! foo.c:40: warning: unknown conversion type character 0xa in format */

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (n = 0; n < 100; n++)
    Well if your file has only 10 records, you end up with 90 records of junk.

    Step through the code with a debugger, and observe what is happening.

    > scanf("&#37;s", &fileName);
    Should be
    scanf("%s", fileName);

    > printf("You entered: %4.2f %\n", annualInterestRate);
    Should be
    printf("You entered: %4.2f %%\n", annualInterestRate);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Hey! thank you would it be the same for:

    scanf("%f",&annualInterestRate);

    i though u needed the & for the address?

    would you know why it does not calculate the totalinterestproperly, it only does in the first loop? x x

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("&#37;f",&annualInterestRate);
    That one is OK - only %s ( and %[ ) into an array is different.

    > would you know why it does not calculate the totalinterestproperly
    Like I said, debug it - step through the code one line at a time, and do the same calculations on paper.
    When the debugger is showing you a different result to the one on your sheet of paper, that means you found a bug.

    Yes, I've got a pretty good idea of where the problem is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Thank you for your help, i've tried lookin more closely at wat is wrong with the program.

    As soon as i tried to calculate the running total:
    totalinterest=totalinterest + dailyinterest no values were correct so i have moved my loops around and thought there was a problem with information being over written so i have stored each daily interest in array this is the new code i have:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    FILE *openmr_Smith;                       /*Declaring a file pointer that will be used to open the file*/
    float initialBalance;
    int n;
    char fileName[50];
    float annualInterestRate;
    int i;
    float currentBalance;
    float charges;
    float dailyInterestRate;
    float dailyInterest[40];
    float totalInterest;
    float min, max;
    float c;
    
    
    struct transactions {
                        int day;
                        int month;
                        int year;
                        float transaction;
                        };
    struct transactions Data[100];
    
    printf("Please enter the filename: ");
    scanf("%s",fileName);
    
    openmr_Smith=fopen((fileName),"r");     /*Open for reading*/
    if (openmr_Smith==NULL) {
    printf("This file name is not recognised");   /*Checking file is valid*/
    } else {
    	printf("Please enter the annual interest rate: "); /*Asking using to enter the annual interest rate*/
    	scanf("%f",&annualInterestRate);                 /*Telling the scanf function to read in a variable of type float and the location of the variable*/
    	printf("You entered: %4.2f%%\n",annualInterestRate);     /*Confirming with the user the annual interest rate they entered*/
    	getchar();
    
    	printf("Opening file: %s ...\n",fileName);
    	fscanf(openmr_Smith, "%f", &initialBalance);
    	
    	for (n=0;n<100;n++)
    	{
    		fscanf(openmr_Smith, "%d/%d/%d %f", &Data[n].day, &Data[n].month, &Data[n].year, &Data[n].transaction);
    	}
    	fclose(openmr_Smith);	
    	
    	currentBalance=initialBalance;
    	/*charges=0;*/
    	min=initialBalance;
    	max=initialBalance;
    	dailyInterestRate=((log((annualInterestRate/100)+1))/3.65);
    	totalInterest=0;
    	c=0;
    
    	for (i=1;i<31;i++)
    	{
    		for (n=0;n<100;n++)
    		{
    		if(i==Data[n].day)
    		{
    		currentBalance=currentBalance+Data[n].transaction;
    		
    		if((Data[n].transaction<0)&&(currentBalance<0))
    		{
    		currentBalance=currentBalance-10;
    		/*c=c+10;*/
    		printf("%f\n",c);
    		}
    		}
    		}
    
    		if(currentBalance>0)
    		{
    		dailyInterest[i]=floor((currentBalance/100)*dailyInterestRate*100);
    		totalInterest=totalInterest+(dailyInterest[i]/100);
    		}
    		
    		if(currentBalance<min)
    		{
    		min=currentBalance;
    		}
    
    		if(currentBalance>max)
    		{
    		max=currentBalance;
    		}
    
    		printf("%4.2f\n",currentBalance);
    		printf("%f\n",totalInterest);
    	}
    
    printf("Start balance: %4.2f\n",initialBalance);
    printf("End balance: %4.2f\n",currentBalance);
    printf("Maximum balance: %4.2f\n",max);
    printf("Minimum balance: %4.2f\n",min);
    printf("Interest paid: %4.2f\n",totalInterest);
    printf("Charges incurred: %f\n",c);
    }
    return 0;
    }
    This now works correctly, but i ave exactly the same problem with calculating the charges. i ave been able to deduct the charges from the current balance and it works fine but i tried using c=c+10; to gain the charges as a figure but again this "running total method" as made all the numbers become completely different i tried to do wat i did with the interest but it does work, could any1 offer any help?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How can a bank transaction be < 0?

    Code:
    for (n=0;n<100;n++)
    		{
    		if(i==Data[n].day)
    		{
    		currentBalance=currentBalance+Data[n].transaction;
    		
    		if((Data[n].transaction<0)&&(currentBalance<0))
    		{
    		currentBalance=currentBalance-10;
    		/*c=c+10;*/
    		printf("%f\n",c);
    		}
    		} 
    		}
    And please DITCH that &^%$%$#@# indent coding style! It's perfect for making your code a pain in the as*.

    Code:
    for (n=0;n<100;n++)
    {
    	if(i==Data[n].day)
    	{
    		currentBalance=currentBalance+Data[n].transaction;
    		
    		if((Data[n].transaction<0)&&(currentBalance<0))
    		{
    	  	    currentBalance=currentBalance-10;
    		    /*c=c+10;*/
    		    printf("%f\n",c);
    		}
    	}
    }
    Now, tell me what c is? All your other variables are nicely named, and then there's poor dumb float c?

    If you want to abbreviate currentBalance to a shorter currBalance or currBal, great, BUT NOT A FLOAT NAMED c?

    If c is a charge, let's call it charge or overdraftCharge, or just odCharge, but not just c!

    Now I'll hop off my soapbox, and if you'll tell me what your program is EXACTLY doing wrong, we can get to work on it.

    I don't want to read through the whole thread, and then have to infer based on the errors of another variable, what you current problems are with &^%$#@ c.

    Please, tell me specifically, what's wrong with &^%$#@ c, OK? If c is a charge, it should be a negative 10, if I understand how you're looking at the balances, here. A positive charge to a negative account balance, would have the effect of increasing the balance of the account.

    Adak

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    hey! ok, ive been working on my code and ive managed to sort out my last problem. i just ave one problem left! i want this program to work for any bank transactions therefore for any month so i need to be able to identify how many days are in the month therefore how many days to look at

    the code that i have at the moment is:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    FILE *openmr_Smith;                       
    float initialBalance;
    int n;
    char fileName[50];
    float annualInterestRate;
    int i;
    float currentBalance, dailyInterestRate, dailyInterest[40], totalInterest, min, max;
    float charges;
    float endBalance;
    int thisMonth;
    int daysInMonth;
    int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    struct transactions {
                        int day;
                        int month;
                        int year;
                        float transaction;
                        };
    struct transactions Data[100];
    
    printf("Please enter the filename: ");
    scanf("%s",fileName);
    
    openmr_Smith=fopen((fileName),"r");    
    if (openmr_Smith==NULL) {
    printf("This file name is not recognised");   
    } else {
    	printf("Please enter the annual interest rate: "); 
    	scanf("%f",&annualInterestRate);                 
    	printf("You entered: %4.2f%%\n",annualInterestRate);     
    	getchar();
    
    	printf("Opening file: %s ...\n",fileName);
    	fscanf(openmr_Smith, "%f", &initialBalance);
    
    	for (n=0;n<100;n++)
    	{
    		fscanf(openmr_Smith, "%d/%d/%d %f", &Data[n].day, &Data[n].month, &Data[n].year, &Data[n].transaction);
    	}
    	fclose(openmr_Smith);	
    
    	thisMonth=Data[0].month;
    	daysInMonth=monthDays[thisMonth-1];
    	printf("%i\n",daysInMonth);
    
    	currentBalance=initialBalance;
    	charges=0;
    	min=initialBalance;
    	max=initialBalance;
    	dailyInterestRate=((log((annualInterestRate/100)+1))/3.65);
    	totalInterest=0;
    
    	for (i=1;i<=daysInMonth;i++)
    	{
    		for (n=0;n<100;n++)
    		{
    		if(i==Data[n].day)
    		{
    		currentBalance=currentBalance+Data[n].transaction;
    		
    		if((Data[n].transaction<0)&&(currentBalance<0))
    		{
    		{
    		currentBalance=currentBalance-10;
    		}
    		charges=charges+10;
    		printf("%f\n",charges);
    		}
    		}
    		}
    
    		if(currentBalance>0)
    		{
    		dailyInterest[i]=floor((currentBalance/100)*dailyInterestRate*100);
    		totalInterest=totalInterest+(dailyInterest[i]/100);
    		}
    		
    		if(currentBalance<min)
    		{
    		min=currentBalance;
    		}
    
    		if(currentBalance>max)
    		{
    		max=currentBalance;
    		}
    
    		printf("%4.2f\n",currentBalance);
    		printf("%f\n",totalInterest);
    	}
    endBalance=currentBalance+totalInterest;
    printf("Start balance: %4.2f\n",initialBalance);
    printf("End balance: %4.2f\n",endBalance);
    printf("Maximum balance: %4.2f\n",max);
    printf("Minimum balance: %4.2f\n",min);
    printf("Interest paid: %4.2f\n",totalInterest);
    printf("Charges incurred: %4.2f\n",charges);
    }
    return 0;
    }
    i have highlighted the bits in orange what im looking at, its does produce the correct number of days "30" because im looking at april but as soon as i put it in the loop the information im printing off is no longer correct. But if i type "30" in the loop it works fine would there be any reason for this?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What happened to all the nice indentation in my previous reply?
    It would be a lot easier to read and debug if the indentation was better.

    > log
    Are you sure you're getting logarithms to the correct base?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM