I need help with my project. I don't understand how I would approach the following situation and need some advice/suggestions.

I am using a file to read info necessary for the project and using classes to access the data.

class Car
{
public:
void CarInit(string& make, string& model, string& color, int year);
.
.
.
private:
string make;
string model;
string color;
int year;
};

The file being read will consist of the following:
10 Toyota Rav4 Silver 2004 (10 =number of cars with those specific specs)
2 Acura Integra White 2000
...

I understand that a class represents a single object. So, how would i save the count, or 10 and 2, in this case in order to use it for implementation?

Thanks a bunch!

hi;
do u want to ask how to convert first part of string to int as 10 or 2 ???

Is the class 'Car' meant to represent a single car, or a a kind of car in inventory?

i.e., if there are 2 Acura's are there 2 Car instances or one that says 'I have 2 Acuras'?

What questions do you want to answer once you have this data in classes?

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:


It is very simpple.
Open and read the inventory ASCII file from your disk and store the values in your clas varibales by using one class object...

fileVariableName = fopen("inventory.dat", "rb");
if( fileVariableName )
{
   // continue to rest of the file processing 
   // here including closing the file
	numOfRecordsRead = fread(&ourRecordVar,sizeof(ourRecordVar), 1, fileVariableName);
	while( numOfRecordsRead != 0 )
	{
		// process the data in ourRecordVar here
		numOfRecordsRead = fread(&ourRecordVar, sizeof(ourRecordVar), 1,fileVariableName);
	}

}
else
{
   // an errror message such as:
   cout << "File " << aFileSpecification << " could NOT be opened!!" << endl;
}
fclose(fileVariableName);

----------OR------------
Simply you run this snippet of code and it may solve your problem....
----------OR------------

#include <stdio.h>     // FILE structure
#include <conio.h>     // clrscr(), getch()
#include <iostream.h>  // cin, cout objects & methods

// The data was stored using this structure
struct product
{
   char productNumber[10];     // a nine digit number 
   char description[31];       // up to 30 characters for description
   float cost;
   int quantityOnHand;
};

void main(void)
{
   product productRecord;
   FILE* PRODUCTS;
   int recordsRead;
   
   float totalValue;

   clrscr();

   PRODUCTS=fopen("products.dat", "rb");
   if ( PRODUCTS )
   {
      // File has been successfully opened, we can try to read data

      recordsRead = fread(&productRecord,
                          sizeof(product),1,PRODUCTS);
      while ( recordsRead != 0 )
      {
         // We have data available to process
         // This is the processing of the individual record data
         cout << endl << endl
              << productRecord.productNumber << " : "
              << productRecord.description << ", "
              << productRecord.quantityOnHand << " @ $"
              << productRecord.cost << endl

         // Calculate the value of this product and add to total
         totalValue = totalValue 
              + (productRecord.quantityOnHand * productRecord.cost);
         
         recordsRead = fread(&productRecord,
                             sizeof(product),1,PRODUCTS);
      }  // end of loop


         // All of the file data has now been processed
         // We can close the file
      fclose(PRODUCTS);

      cout << endl << endl << 
           << "The total value of the inventory is $"
           << totalValue;
   }     // end of normal program 
   else  // File cannot be opened !
   {  
      cout << "Unable to open file" << endl;
   }

   cout << "\nHit any key to quit program execution";
   getch();

}	 // end of main

____________________
Regards,
Rammohan Alampally,
<snip false signature>

commented: >10 posts and still breaking every rule in the book -2

And you think that the orginal poster is still interested after 4 years? And still no code-tags...

Member Avatar for iamthwee

> I am Rammohan from Bangalore and working as a Technical lead in big IT firm.
People who are good don't generally blow their own trumpet.

>Solution for your answer is follows:
I would disagree

> #include <stdio.h> // FILE structure
> #include <conio.h> // clrscr(), getch()
> #include <iostream.h> // cin, cout objects & methods

Erm, is someone using the antiquated version of turbo c and claims to be working for a big IT firm. *shudders* Yes this is a guess, but something tells me I'm correct.


> void main(void)
This is just a no no, and if you were using a decent compiler you would know about this.

> clrscr();
Unportable tripe.


> getch();
Unportable, unnecessary tripe.

We could go on.

> I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Please tell me the name of this firm, so I know to avoid placing any contracts with it in future, or accidentally recommending it to anyone who might be looking for software services.

Yes. I am working in Big IT Firm in Bangalore as a Technical Project Lead.

Either you agree or disagree.
Most of the people who are posting their question over here or sharing their problems are students.
I have posted that code to work on freely available compiler such as TurboC.
Most of the people who work with TurboC is very useful for them to learn more....

____________________
Regards,
Rammohan Alampally,
rammohan@india.com
Technical Lead,
Bangalore, India.
www.FindSyntax.com -> Worlds first Web based Windows O/S is a Brain Chaild of Rammohan

And you think that the orginal poster is still interested after 4 years? And still no code-tags...

Yes, I am thinking it is 100% helpful who search this daniweb for the same type of the problem...

You should encourage the people to post their answers either it is useful or not is the secondary thing....

Good Luck...

____________________
Regards,
Rammohan Alampally,
rammohan@india.com
Technical Lead,
Bangalore, India.
www.FindSyntax.com -> Worlds first Web based Windows O/S is a Brain Chaild of Rammohan

You should encourage the people to post their answers either it is useful or not is the secondary thing....

I would rather emphasize that any answer primarily ought to be useful and that should be considered before posting.

Most of the people who are posting their question over here or sharing their problems are students.
I have posted that code to work on freely available compiler such as TurboC.

But really consider that presumably TurboC was released before most of the students posting here were even born. So, please do not advertise an antiquated/out dated tool for these students. (If anyone is interested in TC, it is available for free from Borland, tagged as antique software ;) )

commented: Sounds like good advice to me! +8

I replied to the wrong thread, Sorry!

"I once did something of that kind.

I wouldl Like to offer my help, but one thing is, "I dont understand your problem". Try to be more specific and if possible send me the project document "

"lets do what we do best!"

> I have posted that code to work on freely available compiler such as TurboC.
No, the problem with your code is that it ONLY works with Turbo C.

Any decent answer shouldn't care about which compiler you use, so long as it's got an "ANSI" badge on it. It's that standard which we strive for here, and it's pretty universal across many of the boards which are not tied to a specific implementation.

Writing code for anything less is just an invitation to "I tried your code with code-muncher 4.2 and it doesn't work".

Besides, what are YOU going to do when you're eventually forced to use another compiler and you discover that your "C" knowledge to date is basically a load of compiler-dependent rubbish and you have to learn all over again.

> You should encourage the people to post their answers either it is useful or not
> is the secondary thing....
The board exists to help students to understand. It is not a forum for those who know something to claim bragging rights for who can produce the best answer to any given student homework. Simply dumping complete answers doesn't do that.

To that end, we try to get students to show what they've done, and then explain where they went wrong and how to fix it. All the while trying to get them to THINK about the problems they face (so they might stand a chance in future). Spoon-feeding a complete answer just because you got bored at lunch serves no one's interests.

Do you really want incapable dolts who rode through college on free homework working next to you in future? I don't, because I've been there - they suck the life out of a team with their ineptitude.

As someone asking a question, I would much rather prefer answers to be short, few, and useful, rather than have everyone post answers and having to sort out which one is correct. In some things there can be debate (style issues and such), but on correctness there is no room for that.

Also - you should only answer recent questions.. not ones that are 4 years old.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.