Thread: Help with a Function Call Program.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    Help with a Function Call Program.

    Hi,

    This is my 1st post, am rather stuck with a problem with my function call program. The program itself shud show 7 Customers and there amount of savings using Intergers and Floats As shown below. The problem is the program itself is only showing 6 Customers. This is racking my brain, can anyone help ?

    Code:
    /* CH1702.cpp */
    /* A program to calculate savings in a building society */
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    void main ()
    {
    /* Description ........
       Open input file
       Display headings
       Initialise total savings and customer number
       Read first customer details
       WHILE customers to process
          Add one to customer number
          Evaluate amount of savings
          Display customer number and savings
          Accumulate total savings
          Read next customer details
       Display total savings
       Close input file
    */
       int number_of_years,
           customer_number ;
       float interest_rate,
             initial_investment,
             customer_savings,
             total_savings ;
       void drawaline (float &, float, float, int);
    
    	FILE *inputfile = fopen ("CH1702.txt" , "r");
    	if (inputfile == NULL)
    
    	{
    
    	printf ("Sorry File Not Found\n");
    	getch ();
    	exit (1);
    
    	}
    
    	printf ("CUSTOMER AMOUNT OF SAVINGS\n");
    	total_savings = 0;
    	customer_number = 0;
    	fscanf (inputfile, "%d" "%f%f\n", &number_of_years, &interest_rate, &initial_investment);
    	while(feof (inputfile) == 0)
    
    	{
    
    	++ customer_number;
    	drawaline (customer_savings, interest_rate, initial_investment, number_of_years);
    	printf ("%4d" "%14.2f\n", customer_number, customer_savings);
    	total_savings += customer_savings;
    	fscanf (inputfile, "%d" "%f%f", &number_of_years, &interest_rate, &initial_investment);
    
    	}
    
    	printf ("Total %13.2f", total_savings);
    	fclose (inputfile);
    	getch () ;
    	}
    
    /*Function For Customers Savings*/
    
    	void drawaline (float & A, float R, float I, int N)
    	{
    /*Description ........
    	Calculate amount of savings
    */
    	A = pow (1 + R / 100, N) * I ;
    	}
    
    
    getch () ;
    Hope i did the tag right by the way as well.

    Ne help would be great btw.

    Daz

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    That is very difficult to read code...

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    First read this and apply to your code!!!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Sorry, about the code, werent 100% on how to post it, am sorry but i dont really get the link micko but, ive used a fscanf for the code. Are you telling me i shud be using fgets ?

    Am really stuck...

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Quote Originally Posted by Micko
    First read this and apply to your code!!!
    One question.... why have u sent me a FAQ on "Why it's bad to use feof() to control a loop" am using a fscanf for each scan, so 4 me too be looking at a feof to control the loop.

    Doesnt make sense really ?

    Daz

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    fear not the tab key. run this.....i didn't fix it, i'll let you do it. i did however, tidy it up.

    if all else fails, try reading the faq about EOF.

    Code:
    /* CH1702.cpp */
    /* A program to calculate savings in a building society */
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    void drawaline (float &, float, float, int);
    
    int main ()
    {
    	int number_of_years;
    	int customer_number;
    	float interest_rate;
    	float initial_investment;
        float customer_savings = 0;
    	float total_savings = 0;
    
    	FILE *inputfile = fopen ("CH1702.txt" , "r");
    	if (inputfile == NULL)
    	{
    		printf ("Sorry File Not Found\n");
    		getch ();
    		exit (1);
    	}
    
    	printf ("CUSTOMER AMOUNT OF SAVINGS\n");
    	
    
    	int fscanf_count = 0;
    	int printf_count = 0;
    
    	fscanf (inputfile, "%d" "%f%f\n", &number_of_years, &interest_rate, &initial_investment);
    	fscanf_count++;
    	printf("Count of fscanf calls: %d\n", fscanf_count);
    	while(feof (inputfile) == 0)
    	{
    		++customer_number;
    		drawaline (customer_savings, interest_rate, initial_investment, number_of_years);
    		//printf ("%4d" "%14.2f\n", customer_number, customer_savings);
    		printf_count++;
    		printf("Count of printf calls: %d\n", printf_count);
    		total_savings += customer_savings;
    		fscanf (inputfile, "%d" "%f%f", &number_of_years, &interest_rate, &initial_investment);
    		fscanf_count++;
    		printf("Count of fscanf calls: %d\n", fscanf_count);
    	}
    
    	printf ("Total %13.2f", total_savings);
    	fclose (inputfile);
    	getch () ;
    
    	return 0;
    }
    
    /*Function For Customers Savings*/
    
    void drawaline (float & A, float R, float I, int N)
    {
    	A = pow (1 + R / 100, N) * I;
    }
    // }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by DazzaE2005
    One question.... why have u sent me a FAQ on "Why it's bad to use feof() to control a loop" am using a fscanf for each scan, so 4 me too be looking at a feof to control the loop.

    Doesnt make sense really ?
    run what i gave ya
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by DazzaE2005
    One question.... why have u sent me a FAQ on "Why it's bad to use feof() to control a loop" am using a fscanf for each scan, so 4 me too be looking at a feof to control the loop.

    Doesnt make sense really ?

    Daz
    Because you're using feof() to control loop!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Right ok, the input file has 7 lines of code and then the program shud add up the code at the bottom displaying the total amount of savings.

    So therefore the code that was but in by misplace, is correct but it the same as the previous code. The program itself is not reading the last line of the input file. That is what I dont understand.

    Daz

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ugg... that's why i put in the printf's

    this is what your code says

    Code:
    (record = a line from the file)
    scan a record
    
    is feof zero?  (while loop 1)
    	no, so print it out
    	scan in another record
    is feof zero?  (while loop 2)
    	no, so print it out
    	scan in another record
    is feof zero?  (while loop 3)
    	no, so print it out
    	scan in another record
    is feof zero?  (while loop 4)
    	no, so print it out
    	scan in another record
    is feof zero?  (while loop 5)
    	no, so print it out
    	scan in another record
    is feof zero?  (while loop 6)
    	no, so print it out
    	scan in another record
    is feof zero?
    	yes, now print out the total savings
    get it?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by DazzaE2005
    The program itself is not reading the last line of the input file.
    that is where you're wrong
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 07:38 PM