Solved

Reading from sequential text file issues

Posted on 2000-04-12
8
198 Views
Last Modified: 2010-04-02
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
Comment
Question by:jkelly061597
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 2710459
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
 

Author Comment

by:jkelly061597
ID: 2710566
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2710600
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2710619
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 350 total points
ID: 2710639
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
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2710655
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
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2712063
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
 

Author Comment

by:jkelly061597
ID: 2713846
There's another Q with like title and points for you, ShuanWilde...

Thanks for your help all,
jeff
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now