Solved

Reading from sequential text file issues

Posted on 2000-04-12
8
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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