nested while loops possible?

This is a discussion on nested while loops possible? within the C Programming forums, part of the General Programming Boards category; sorry for bugging you guys, last question i promise Ok this code needs to 1)read id and quantity records from ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    nested while loops possible?

    sorry for bugging you guys, last question i promise

    Ok this code needs to

    1)read id and quantity records from a newSupply.txt
    2)read id record from inventory.dat
    3)if there is a corresponding id in inventory.dat add newSupply.quantity to inventory.quantity.
    4)if there has been a typo or something in newsupply.txt and no matching id record is found in inventory.dat write incorrect data to exception.txt

    Basically the only way i know to open a file and read it is to loop and read 1 record at a time, so what i tried to do was get the program to

    1)get the first id record in newSupply file
    2)get first id record in inventory file
    3) see if they match, if not loop until a match is found. if a match is found update.
    4)if no match is found the updated variable stays at 0 and data written to exception.txt file
    5)get next id record in newSupply file and repeat until EOF in Supply.txt

    Here is what Ive done so far, which i was quite proud of until I realised it did absolutly nothing


    Code:
    void updateInventory(void)
    {
    	FILE *inFile;
    	FILE *exceptionFile;
    	FILE *supplyFile;
    	parts_struct parts;
    	int updated;
    	updated = 0;
    	newParts_struct newParts;
    	inFile = openFile(inFileName, "rb+");
    	supplyFile = openFile(newSupplyFile, "r");
    	exceptionFile = openFile(exceptionFileName, "w");
    	
    	
    		while(!feof(supplyFile))
    		{
    			newParts = readNewSupply(supplyFile);
    			while(!feof(inFile))
    			{
    				parts = readParts(inFile);
    				if (newParts.id == parts.id)
    				{
    					
    					parts.qtyAvailable = parts.qtyAvailable + newParts.quantity;
    					updated =1;
    					
    					
    				}
    				
    			}
    			if (updated = 0)
    			fprintf(exceptionFile, "%s %-20s %4d", newParts.id, newParts.description, newParts.quantity);
    		}
    I really cant see why this wont work, the inner loop just doesnt seem to be looping, not even once i dont think.

    Im sure there are much better, more correct ways to do this but bare in mind in new.

    Can anyone see where im going wrong?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hmm, perhaps you should go read the FAQ link on why not to use feof for loop conditions. I know you've been shown it already. Also, while you're at it, go read the Announcements. Don't start a new thread for the same problem.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    quzah, i have read the FAQ, but this is the way we have been taught to do it in class and ive never had any problems with it displaying the last line twice like the FAQ says. then again maybe the problems im posting about are related to feof i dont know...

    Sorry about starting a new thread.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > if (updated = 0)
    == not =

    > while(!feof(inFile))
    On reading your 2nd record from supplyFile, will this be true or false?

    Oh, one more thing
    Set your source code editor/IDE to use spaces and not tabs for indenting.
    Tabs may look OK for you, but on the board your code rapidly disappears to the right.
    4 spaces seems quite nice.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Quote Originally Posted by Salem
    > if (updated = 0)
    == not =
    silly mistake
    > while(!feof(inFile))
    On reading your 2nd record from supplyFile, will this be true or false?
    ah..I dont think i understood exactly how !feof worked. Are you saying that once it has run through once it wont go through the innner loop again because it thinks its already at the end of the file?.
    So I need to tell it to to go back to the start of the file.
    Code:
    while(!feof(supplyFile))
    {
        newParts = readNewSupply(supplyFile);
            while(!feof(inFile))
            {
                parts = readParts(inFile);
                if (newParts.id == parts.id)
                   {
    					
                       parts.qtyAvailable = parts.qtyAvailable +newParts.quantity;
                       updated =1;
    					
    					
                    }
    				
             }
              if (updated = 0)
               fprintf(exceptionFile, "%s %-20s %4d", newParts.id, newParts.description, newParts.quantity);
              fseek(inFile, 0L, SEEK_SET);
    }
    Will fseek do the trick?

    Oh, one more thing
    Set your source code editor/IDE to use spaces and not tabs for indenting.
    Tabs may look OK for you, but on the board your code rapidly disappears to the right.
    sorry, will do.

    Appreciate the help, thanks.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Will fseek do the trick?
    Yes, but you also need clearerr(inFile) as well.

    > once it has run through once it wont go through the innner loop again because it thinks its already at the end of the file?.
    Exactly.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Ok im still having problems.

    the code didnt work because my function newParts = readNewSupply(supplyFile) was trying to read a text file like a binary file.

    so Ive changed it to just use Fgets to read the strings but when I try and read the first record wich is 6 digits long it reads the entire record instead. so instead of just displaying the ID which is something like abc123 it displays the ID, description and quantity records.

    Code:
    typedef struct
    {
    	char id[7];
    	char description[26];
    	int quantity;
    }newParts_struct;
    Code:
    void updateInventory(void)
    {
    	FILE *inFile;
    	FILE *exceptionFile;
    	FILE *supplyFile;
    	parts_struct parts;
    	newParts_struct newParts;
    	inFile = openFile(inFileName, "rb+");
    	supplyFile = openFile(newSupplyFile, "r");
    	exceptionFile = openFile(exceptionFileName, "w");
    	
    	
    		while(!feof(supplyFile))
    		{
    			newParts = readNewSupply(supplyFile);
    			
    			while(!feof(inFile))
    			{
    				parts = readParts(inFile);
    				if (strcmp(newParts.id, parts.id) == 0)
    				{
    					printf("Updated");
    				}
    			}
    		}
    
    
    	
    	closeFile(inFile);
    	closeFile(exceptionFile);
    	closeFile(supplyFile);
    }
    Code:
    newParts_struct readNewSupply(FILE *supplyFile)
    {
    	newParts_struct newParts;
    	fgets(newParts.id,7,supplyFile);
    	printf("%s", id);	
                    return newParts;
    
    }
    All im trying to do first is get the first 6 digits which is the id then ill go on from there but it displays the whole file.

    what annoys me is that I can get fgets to read exactly how i want it to if I start a new file, but the exact same code wont work in this program.

    Any ideas?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I bet you aren't null terminating your line, are you?
    Code:
    ewParts_struct readNewSupply(FILE *supplyFile)
    {
    	newParts_struct newParts;
    	fgets(newParts.id,7,supplyFile);
    	printf("%s", id);	
                    return newParts;
    
    }
    Oh, and the above code is wrong anyway. You're printing id, but you're reading into newParts.id. I'll assume that's a typo. :P

    At any rate, try making "newParts.id[6]" a null.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    You're printing id, but you're reading into newParts.id. I'll assume that's a typo. :P
    yeah that was a typo.

    I bet you aren't null terminating your line, are you?
    not sure what you mean by this.

    At any rate, try making "newParts.id[6]" a null.
    you mean when I decale newParts.id in my structure?. make it null instead of a char?

    what i dont understand is why if i stick the code into a new file and compile it reads just the id like I want, but in the complete program it reads the whole dam line from the file.

    thanks for the help quzah.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    ok im getting somewhere.

    Code:
    newParts_struct newParts;
    	char id[7];
    	char description[26];
    	int quantity;
    	fscanf(supplyFile, "%6c %25c %d ", id, description, &quantity);
    	id[6] = '\0';
    	description[25]= '\0';
    	printf("%s\n", id);
    	return newParts;
    ok almost fixed it.

    its reading correctly now but messing up when it gets to the quantity record and there is a typo such as $00 instead of 400 ot 1oo instead of 100. the line directly after that is messed up

    EDIT:
    nevermind fixed it, made quantity a char instead of int.
    Last edited by prepare; 10-13-2004 at 10:51 PM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    ok this is the last hurdle. I need to perform an integer validation check. Now before anyone tells me to search, i have but couldnt find anything I understoof 100%, or even 50% and i really dont want to put a whole lot of code that works but i have NFI about in my program.

    Code:
    newParts_struct readNewSupply(FILE *supplyFile, FILE *exceptionFile)
    {
    	newParts_struct newParts;
    	char quantity[4];	
    	fscanf(supplyFile, "%6c %25c %4s ", newParts.id, newParts.description, &quantity);
    	newParts.id[6] = '\0';
    	newParts.description[25]= '\0';
    	if ( scanf ( "%d", &quantity ) != 1 )
     	 {
        		fprintf(exceptionFile, "%6s %-25s %4s\n", newParts.id, newParts.description, newParts.quantity);
       	 }
    	else if (scanf("%d", &quantity ) == 1 )
    	{
    		newParts.quantity[4] = quantity[4];
    	}
    This code is meant to just check the value of quantity to make sure its in integer, and if it isnt write to the error file.
    the lines in read I know are errors I just cant remember or find the command to read a variables contents rather then read user input.

    I really appreciate all the help guys. Have learnt alot in the last week since i started and i couldnt even open a binary file.

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    im not sure what you mean
    Code:
    if ( isdigit(quantity[i]) )
    {
        ...
    }
    like that?

    quantity is an array so to check all elements you will need a for loop

    (make sure
    Code:
    <string.h>
    is included)
    Last edited by sand_man; 10-14-2004 at 04:41 AM.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > char quantity[4];
    > if ( scanf ( "%d", &quantity ) != 1 )
    Does your compiler even complain about that?

    Code:
    int result;
    char quantity[5];	// %4s will append a \0
    fscanf(supplyFile, "%6c %25c %4s ", newParts.id, newParts.description, quantity);
    newParts.id[6] = '\0';
    newParts.description[25]= '\0';
    if ( sscanf ( quantity, "%d", &result ) != 1 )// sscanf, not scanf
     {
    	fprintf(exceptionFile, "%6s %-25s %4s\n", newParts.id, newParts.description, newParts.quantity);
     }
    else
     {
    	strcpy(newParts.quantity, quantity );  // its a string - use strcpy
     }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    im guessing that i didnt understand the post properley

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're reading an integer into a character array. Your compiler should warn you about that. If it doesn't, try compiling with your warnings turned on or to a higher level.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  2. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  3. nested for loops and bank account interest help!!
    By webvigator2k in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 08:03 PM
  4. Output from nested loops
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 06:09 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 09:25 AM

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