C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 20th, 2004, 03:52 PM
bobert bobert is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Colorado, USA
Posts: 15 bobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 sec
Reputation Power: 0
C/C++: Searching for unique items in a struct

Hi!

In C/C++, am trying to put only the unique names from one struct into another struct that will have only the unique names (no duplicates). For example, I have a list of cars that has only one piece of info: the make of the car. Here's an example list of cars:

Code:
Toyota
Jaguar
Toyota
Nissan
Chevrolet
Ford
Jaguar
Nissan
Toyota
Toyota


As you can see, there can be more than one instance of the make of the car in this list. Now, I want to fill a new struct with a unique list of the cars and the number of instances of each car maker. Therefore, I have a struct that is like this:

Code:
struct
{
  char make[16];
  int numberofcars;
} cars[30];


So, after processing the list and filling the struct, I want the struct to have these values:

Code:
cars[0].make = "Toyota"
cars[0].numberofcars = 4

cars[1].make = "Jaguar"
cars[1].numberofcars = 2

cars[2].make = "Nissan"
cars[2].numberofcars = 2

cars[3].make = "Chevrolet"
cars[3].numberofcars = 1

cars[4].make = "Ford"
cars[4].numberofcars = 1
 .
 .
 .
cars[29].make = ...
cars[29].numberofcars = ...


Is there an easy way to do this using "while loops", "for loops" and "if statements"? I know how to do string comparisons and copy strings into the struct, but the logic I have tried is not working. :-) Oh, and sorting would be nice, but isn't required for my purposes.

Thanks,

bobert

Reply With Quote
  #2  
Old February 22nd, 2004, 01:15 AM
bobert bobert is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Colorado, USA
Posts: 15 bobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 sec
Reputation Power: 0
Someone else gave me a hint on another website and it's working now. This is what he said:

Lets assume you have another variable: carCount that actually tracks the number of cars in use. Then you could write something like the following (note: I am reading from cin; you can easily replace that with your input stream).

Code:
struct CarStruct
{
   char make[16];
   int numberofcars;
};

CarStruct cars[30];

int findCar(const CarStruct *arrayOfCars, int carCount, string nameOfCar);
int insertCar(const CarStruct *arrayOfCars, int &carCount, string nameOfCar);

string nameOfCar;

while (cin >> nameOfCar)
{
   // will search the list from beginning for carCount entries for named car.
   // returns -1 if not found, index of car if found
   int carNdx = findCar(cars, carCount, nameOfCar);
   if (carNdx < 0)
   {
      // adds new entry by incrementing carCount, setting name and zeroing
      // number of that make of car. Returns index of newly added car
      carNdx = insertCar(cars, carCount, nameOfCar);
   }
   cars[carNdx].numberOfCars++;
}


bobert

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C/C++: Searching for unique items in a struct


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT