Thread: Comparing a struct array...

  1. #1
    Registered User
    Join Date
    Oct 2008

    Comparing a struct array...

    I'm writing a program that reads some files. The first file contains a list of data for books, and the second file contains information for markets from where the books are bought. The first file has the information for the market that the particular book was purchased...

    The whole purpose for is to sort the book list by market, which I had no trouble doing. It goes something like this:

    for (c = 1; c < MARKETS; c++)
    		for (i = 1; i < BOOK_IN_LIST; i++)
    			if (bArray[i].market == mArray[mkt].market)
    		        input_to_file << bArray[i].item_name << endl;
    mkt = mkt + 1
    So it reads the nth market and checks the name against every market from the book data file, and inputs it to a file in sorted order with appropriate headers/footers.

    The point where I'm drawing a blank is that some of the books do not have a market associated with it, or contain invalid text. If these markets are not in the book source/market file then I need to group them all together and list them as being invalid and needing to be fixed.

    Any Ideas? I'm just suffering from (novice) programmer's block at the moment... haha.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Did you overload the == operator?

    for market?

    If this method doesn't work for comparing markets.

    Why not just add an integer or something, whenever a book has a market.

    Or better yet, check if bArray[i].market == NULL... to see if it has been set.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    The market could have a "invalid" value, such as 0, for books that have no market.
    (Since I assume market is just an integer of sorts, or something?)
    If it's a class, then you can overload operator == and keep track of which books have a market or not (ie a member variable, m_HasMarket or something).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    train spotter
    Join Date
    Aug 2001
    near a computer
    >>some of the books do not have a market associated with it, or contain invalid text

    So I assume the 'market' field contains text type data (char array or string).

    If 'market' is a string type class (CString, String etc) your code will work (as these classes define an equals operator) otherwise try 'strcmp' or similar (to compare char arrays).

    I have added a flag to find books without a market. What you do with them is up to you.....
    for (c = 1; c < MARKETS; c++)
    	for (i = 1; i < BOOK_IN_LIST; i++)
    		if (strcmp (bArray[i].market, mArray[c].market) == 0)
    		        input_to_file << bArray[i].item_name << endl;
    	//did we find a market?
    	if(bFound==false)//no market
    		//handle no market found
    Is there a reason you are starting at one not zero? (C++ arrays start at 0 to num elements -1)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Multidimensional struct array help
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 08-22-2006, 02:26 PM
  4. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM