Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Reading from sequential text file issues

Visit the following directory and get the contained files:

http://library.thinkquest.org/10867/fleet/

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?

thanks,
jeff
0
jkelly061597
Asked:
jkelly061597
  • 3
  • 2
  • 2
  • +1
1 Solution
 
graham_kCommented:
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??

0
 
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...
0
 
graham_kCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
graham_kCommented:
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).
0
 
AndrewRodionovCommented:
Hi jkelly,

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

Vehicle myCar3(fin);
myCar2.viewCar(cout);
     ^

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.

Regards.
Andrew.
0
 
AndrewRodionovCommented:
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?).
0
 
ShaunWildeCommented:
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

so

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

etc


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

Thanks for your help all,
jeff
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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