Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Reading from sequential text file issues

Visit the following directory and get the contained files:

This program is a driver for the Vehicle class (provided) which I'm currently testing. My problem is easily seen when executing. The intent is to have three vehicle listed. Instead I get one vehicle and two that contain the identical junk.

How can I avoid this problem and read in the second two cars for display?

Any other general recomendations?

  • 3
  • 2
  • 2
  • +1
1 Solution
without goijng into it *too* deeply - why are you handling myCar1 differently from myCar2 & myCar3 ??   You create myCar1 with the Vehicle (void); constructor, which seems to work o.k, but you then create myCar2 & myCar3 with the Vehicle (fstream&) constructor.

Why not treat myCar2 & myCar3 like you did myCar1 ?  Just make a function to avoid repeating the code (of course, you could stand it on its head & construct myCar1 with the Vehicle (fstream&) constructor, but it looks like that's not working).

So, do you want an explanation of *why* it won't work, or do you just want to be told that if you treat the 2nd & 3rd lkike the first, then it *will* work??

jkelly061597Author Commented:
The prog1.cpp file is a driver for testing the Vehicle class. The reason the other constructors exist is because for the implementation of the class they will be required. The reason the instances are handled differently is because the code is just a progression of steps used in testing the class.

I need to get the file input working.

Thanks for your help...
o.k, then now that you've proven the non file based version with myCar1, it's time to use the file based for all.

I'll d/l the code & see if I can take a look at it.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

hmm, looks like you're using MS VC++ or some such. I tried it with Gnu C++ on a Solaris (Unix machine) and it won't link.

I've got to go to a meeting right now. Will try something else later. Btw, maybe you ought to use "new" when constructing myCar2 & 3 (I'm not sure, though. Just a guess).
Andrei RodionovCommented:
Hi jkelly,

First of all, there is a small misprint in your prog1.cpp

Vehicle myCar3(fin);

Obviously, you mean myCar3.viewCar(cout);

Now about the junk. Look at your core member function Vehicle::setCar. That is your code:

// each of the following are pointers thus memory must be allocated
vmake = new char[strlen(make)];
vmake = make;

vmodel = new char[strlen(model)];
vmodel = model;

vstate = new char[strlen(state)];
vstate = state;

vplate = new char[strlen(plate)];
vplate = plate;

vcolor = new char[strlen(color)];
vcolor = color;

You assign one pointer to another! There is *no* copying. So try strcpy( vmake, make ); instead vmake = make; (and for vmodel, vstate, vplate and vcolor also) and you'll be successful.

Andrei RodionovCommented:
And some more. I've tested the code under Mingw32 GCC-2.95.2.
jkelly's original code have produced no junk but blanks (that's better?).
as a follow on from AndrewRodionov comment you should use strcpy to copy your data but also you are not making room for your '\0' string terminator


vmake = new char[strlen(make)+1];


jkelly061597Author Commented:
There's another Q with like title and points for you, ShuanWilde...

Thanks for your help all,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now