Thread: Function help

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    9

    Function help

    Allight here is my function i am getting a negative output for Track revenue . the number is right but it negative. How do i fix it and also who do i shorten/ make my code more efficient. Thanks

    Code:
    void writers (int cid [ ], double cidvalue [ ], int count)
    {
    	FILE* dancef = fopen ("tracks.dat", "rt");
    	FILE* annf = fopen ("recp.rpt", "wt");
    	int numvtracks = 0;
    	int numitracks = 0;
    	int numl, cidnum1, cidnum2;
    	double track1, track2, tprice;
    	double total = 0.0;
    	int ftrack1, ftrack2;
    	char sPin [16];
    	while (count > 0)
    	{
    		numl = fscanf (dancef, "%d %d %12s\n", &cidnum1, &cidnum2, &sPin);
    		if (numl != 3) break;
                    fprice1 = search (cid, cidvalue, count, cidnum1, &track1);
    		fprice2 = search (cid, cidvalue, count, cidnum2, &track2);
    		if (ftrack1 && ftrack2 && (cidnum1 != cidnum2))
    		{
    			tprice =  (track2 - track1);
    			total += tprice;
    			numvtracks++;
    			fprintf (annf, "%s   %2d   %2d   $%5.2lf\n", sPin, cidnum1, cidnum2, tprice);
    		}
    		else
    		{
    			fprintf (annf, "INVALID DATA: %2d  %2d *\n", cidnum1, cidnum2);
    			numitracks++;
    		}
    	}
    	fclose (annf);
    	fclose (dancef);
    	printf ("\ntracks processed: %d\n", numvtracks);
    	printf ("invalid tracks processed: %d\n", numitracks);
    	printf ("Track revenue: $%.2lf\n", total);
    Last edited by jafa401; 08-09-2009 at 05:11 PM.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    I'm going to take a shot at this despite very weak knowledge of C and programming in general.

    It seems that track revenue is stored in the variable 'total' which is calculated from substracting 'track2' from 'track1'.

    However, as I see from your code you have not stored anything in those two variables, nor have you even initialized them. If you don't initialize a variable, the existing data in the peice of memory that has been allocated will still be there. Therefore yielding undefined results when any sort of computation is performed upon them or with them.

    I have absolutely no idea what your goal is because your code is so obscure to me, but I'm pretty certain that what I've stated is why your getting negative results.
    Last edited by Meikj; 08-09-2009 at 04:44 PM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    sorry my bad i forgot 2 lines of code
    i have updated
    thanks for the help

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > while (count > 0)
    Use a debugger, put a breakpoint on this line.

    When you hit the breakpoint, add say 'total' to a watch window, then single step the code and observe.

    When expectation != reality, you've found a bug.
    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
    Aug 2009
    Posts
    9
    Quote Originally Posted by Salem View Post
    > while (count > 0)
    Use a debugger, put a breakpoint on this line.

    When you hit the breakpoint, add say 'total' to a watch window, then single step the code and observe.

    When expectation != reality, you've found a bug.
    i dont have a debugger

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    And we don't have all your code, so I guess we're all out of luck.

    Which compiler are you using?
    Most compilers of any usefulness come with a debugger.
    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
    Aug 2009
    Posts
    9
    i figure how get the correct answer i used fabs (track2 - track1)
    its solves the problem

    ok I want to make my code more efficient and tight
    i feel like i could do this code in less number of lines
    i just can't figure out how

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jafa401 View Post
    ok I want to make my code more efficient and tight
    i feel like i could do this code in less number of lines
    i just can't figure out how
    IMO the code is fine already. The flow of execution makes sense, it's readable, there are no obvious inefficiencies or pointless quirks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM