Solved

Reading from sequential text file issues

Posted on 2000-04-12
8
202 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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