Stuck in a loop!.....Get me out of here!!

This is a discussion on Stuck in a loop!.....Get me out of here!! within the C Programming forums, part of the General Programming Boards category; I have written a program for my coursework and I seem to be caught in a loop when I try ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    Stuck in a loop!.....Get me out of here!!

    I have written a program for my coursework and I seem to be caught in a loop when I try to compile and run.
    I am kinda new to C and I was wondering if perhaps someone can help me out.

    My assignment was to write a program which accepts a TEXT file input from a shopkeeper which updates his stock records each week, his stock record is held on another TEXT file.

    The existing stock file is in product code number order, a 4 digit product code, 4 digit quantity code ( 1000 -9999), 4 digit buy price 4 digit sell price .

    My anendment file is 4 digit product code, 1 character amendment type, 4 digit value.

    I have to create a new stock record in the same order as the original, updating any products with any item outside the range being written to a query file,


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void query_files (FILE *new_stock_file, FILE *query_file);
    void amend_details (char type, int *amount, int *buy_price, int *sell_price, int value);
    
    int main ()
    {
    	FILE 	*stock_file			= NULL,
    			*amend_file			= NULL,
    			*query_file 		= NULL,
    			*new_stock_file		= NULL;
    
    	int 	code = -1,
    			amount = 0,
    			buy_price = 0,
    			sell_price = 0,
    			amend = 0,
    			value = 0,
    			amend_2 = 0;
    
    	char	type			= NULL,
    			type_2		= NULL;
    
    	/*Open files ready for processing*/
    	if ( (stock_file = fopen("stock.dat","rt")) == NULL  ||       	/*Read*/
    		  (amend_file = fopen("amend.dat","rt")) == NULL  ||       	/*Read*/
    		  (new_stock_file = fopen("new_stock.dat","wt")) == NULL ) 	/*Write*/
    	{	printf("Error in Opening Files for Amendment Processing.\n");
    		exit(1);
    	}
    
    	/*Get amendment file details*/
    	fscanf(amend_file,"%d,%c,%d",&amend,&type,&value);
    
    	/*Store values needed for writing to new stock file*/
    	amend_2 = amend;
    	type_2 = type;
    
    	/*Process files until amendment code is equal to 9999 */
    	while(amend != 9999)
    	{	/*Get next line from stock file while the product code is less than */
    		/*that of the amend files product code.                             */
    		while ( code < amend )
    		{	fscanf(stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
    			/*If no amendments to current product, add product to new file */
    			if (code != amend && code >= 0)
    				fprintf(new_stock_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
    		}
    
    		/* Amend stock file values */
    		amend_details(type,&amount,&buy_price,&sell_price,value);
    
    		/*Get next amendment file details*/
    		fscanf(amend_file,"%d,%c,%d",&amend,&type,&value);
    
    		/*If there are no more amendments for the current product then write */
    			/*the new values to the new stock file                               */
    		if (amend != amend_2)
    		{	if (type_2 != 'D')  /*Do not write a product marked for deletion*/
    				  fprintf(new_stock_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
    			type_2 = type;
    			amend_2 = amend;
    		}
    	}
    
    	/*Add the remainder of stock file to new file*/
    	while ( code != 9999 )
    	{	fscanf(stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
    		if (code == 9999)
    			fprintf(new_stock_file,"9999\n");
    		else
    			fprintf(new_stock_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
    	}
    
    	/* Close files after processing */
    	fclose(stock_file);
    	fclose(amend_file);
    	fclose(new_stock_file);
    
    	/*Open files ready for processing */
    	if ( (query_file = fopen("query.dat","wt")) == NULL  ||        /*Write*/
    		  (new_stock_file = fopen("new_stock.dat","rt")) == NULL )  /*Read*/
    	{	printf("Error in Opening Files for Query Processing.\n");
    		exit(1);
    	}
    
    	/* Query file - Check if any stock item is outside the given range */
    	query_files(new_stock_file, query_file);
    
    	/* Close files after processing */
    	fclose(query_file);
    	fclose(new_stock_file);
    	return EXIT_SUCCESS;
    }
    
    void amend_details (char type, int *amount, int *buy_price, int *sell_price, int value)
    {	switch (type)
    	{	case 'I':
    			/* process Issue from stock.*/
    			*amount = (*amount - value);
    			break;
    		case 'R':
    			/* process Receipt to stock.*/
    			*amount = *amount + value;
    			break;
    		case 'B':
    			/* process Buying price amendment.*/
    			*buy_price = *buy_price + value;
    			break;
    		case 'S':
    			/* Process Amendment - Sale price.*/
    			*sell_price = *sell_price + value;
    			break;
    		case 'D':
    			/* Do not write current entry to new stock file */
    			/* Process Amendment - Delete stock item.*/
    			break;
    		default:
    			printf("Illegal Amendment Character Program Ending.\n");
    			exit(EXIT_FAILURE);
    	}
    }
    
    void query_files (FILE *new_stock_file, FILE *query_file)
    {	int 	code = -1,
    			amount = 0,
    			buy_price = 0,
    			sell_price = 0;
    
    	fscanf(new_stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
    
    	while (code != 9999)
    	{	if (amount < 1000 || amount > 9999)
    			  fprintf(query_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
    			  /* Write to query file if product code is out of range */
    		fscanf(new_stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
    
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    I have used 2 dummy text files containing stock and stock amendments and used step over on my compiler and I still get caught in a loop....very frustrating.

    Anyone got any advice>?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    I just double checked my text files for amendments and stock and found a few errors..( dam those commas!!.....lol), re compiled used step over and hey presto!.....got a working prog!....woohoo.

    Unfortunately I now have a query file spat out my new stock file is correct, but I have a query file with all my new stock file records.

    Stock Record Text File

    0001,0020,0799,0999
    0002,0021,0899,0999
    0003,0022,0999,1099
    0004,0023,1099,1199
    0005,0024,1199,1299
    0006,0035,3999,4400
    0007,0036,3899,4500
    9999

    Amendment Stock Text File

    0001,I,0004
    0002,R,0004
    0003,S,0100
    0004,B,0200
    0005,D
    9999

    Query Stock File Record

    0001,0016,0799,0999
    0002,0025, 899,0 999
    0003,0022, 999,1199
    0004,0023,1299,1199
    0006,0035,3999,4400
    0007,0036,3899,4500

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>fscanf(amend_file, "%d,%c,%d", &amend, &type, &value);
    Always check the return code from fscanf() to ensure it read the correct number of items. In this case it should return 3.

    How many entries are in your amend file? If there's more than 1, then I presume you'll need to consider how to process them all. By this, I mean you need to remember that you have already read in the stock_file and that file pointer will possibly now be at EOF. Also, when writing to new_stock_file, the same care must be taken when processing more than one amend entry. (I haven't read through all the logic in your code, maybe you've already done this?)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21