Posts Tagged ‘structures’

Help with c++ assignment- structures and data files – causes segmentation fault?

Wednesday, September 21st, 2011

Question by Yoni R: Help with c++ assignment- structures and data files – causes segmentation fault?
include
#include
#include
using namespace std;

struct Cars // Cars structure
{
string name;
string make;
string model;
string drive_train;
string color;
int miles;
};

int main(int argc, char *argv[])
{
char ch;
string datatypechoice;
cout << "Press one of the following numbers followed by enter to choose what to do:\n\n1. Add to Cars\n2. Read from Cars\n"; cin >> datatypechoice;
if (datatypechoice == “1”)
{
Cars data;
cout << "Enter the name of the car: "; cin >> data.name;
cout << "Enter the make of the car: "; cin >> data.make;
cout << "Enter the model of the car: "; cin >> data.model;
cout << "Enter the drive train of the car: "; cin >> data.drive_train;
cout << "Enter the color of the car: "; cin >> data.color;
cout << "Enter the amount of miles on the car: "; cin >> data.miles;

ofstream userinput (“cardatabase.dat”, ios::binary | ios::app);

userinput.write((char *)&data, sizeof(data));

userinput.close();
}
else if (datatypechoice == “2”)
{

Cars data;
ifstream readdat (“cardatabase.dat”, ios::binary);

if(!readdat)
{
cout << "Cannot open file."; return 1; } readdat.read((char *)&data, sizeof(data)); cout <<"Name:" << data.name << endl << "Make:" << data.make << endl << "Model:" << data.model << endl << "Drive train:" << data.drive_train << endl << "Color:" << data.color << endl << "Miles:" << data.miles << endl; readdat.close(); } else { cout << " NOT AN OPTION!!!!!!!!!!!!!!1111111111111\n\n\n"; } cout << endl; return 0; } That is my code. When i run it and hit 1 it works, but when i run it again and choose 2 it segmentation faults. HELP!!! the dat file is nonexistant 'til u create it with the prog Best answer:

Answer by LoverOfWine
Since we don’t have your dat file it is difficult to see but I would highly suspect you have extra characters in your file or it isn’t matching the format of your structure. Unless you know exactly how the structure is organized in memory (especially with objects embedded in it) that is very unsafe.

Rather than read the whole structure in at one time, break it up by field and see which one is causing your problems.

Also, reading over top of a string object probably isn’t good either.

Add your own answer in the comments!

Build a model train layout: Model railroad buildings and structures how to WGH

Friday, May 13th, 2011

This video series, hosted by Michael Gross, will teach you the simple how-to workshop techniques you need to know to take an HO scale electric train set and make it a scenic model railroad. Well show you how to build a 4×8 model train table (benchwork) from plywood, lay sections of track, create easy realistic scenery using foam and paint, assemble model buildings, and wire your layout with a DC power pack. When youre done, youll have a railroad that operates well and looks real. Our techniques would work in N, O, S, G, or Z scale also. This video is scene 6, Structures how-to. For more information and a free booklet with more tips visit greatesthobby.com. The Worlds Greatest Hobby program is a non-profit organization dedicated to promoting the fun, relaxing, and fascinating hobby of model railroading. Primary sponsors of our series are Atlas Model Railroad Co., Bachmann Trains, Kato Precision Railroad Models, Wm. K. Walthers Inc., and Kalmbach Publishing. Co, publisher of Model Railroader and Classic Toy Trains magazines. To contact the World’s Greatest Hobby e-mail Info@GreatestHobby.com Visit our website at www.greatesthobby.com

What are the ramifications of running electric bullet trains along existing power line structures in the US?

Saturday, May 7th, 2011

Question by Serverine: What are the ramifications of running electric bullet trains along existing power line structures in the US?

Best answer:

Answer by Curious George
The only existing power structures that trains can use are in the “North East Corridor” (DC to NY). We already have our own US version of bullet trains operating under these power lines that goes by the name of “Acela” and Amtrak operates it.

Granted the Acela is no where near the speed of a foreign bullet train. Top speed for our trains is 150mph, but they rarely actually travel that fast. To further High Speed Rail in this country would involve adding more infrastructure. Either by building new roadbed and power systems, or updating current trackage in certain areas to handle high speeds and adding the power systems to those.

America isnt really sold on train travel like it used to be before the 1960s. So there isnt much interest in HSR. The idea of the heavy amount of work to be done to bring HSR, not to mention the cost (same as bailing out a car manufacturer) is still prohibitive as plane travel already has this part of the market cornered for the next forseeable future (25-50 years).

As far as any ramifications, just the building of an HSR would be the only bad things. The operation of such, would certainly make its worth, justifiable. Still, its apparent the US would rather buy new cars and plug major oil spills than to enter into things like fusion power generation and green high speed trains.

In essence, the only ramification of this is truely, the american public. When are we gonna get off our collective kiesters and just get the job done!

Know better? Leave your own answer in the comments!

Subscribe to RSS feed

Dallas Cowboys vs. New York Jets    Watch Pittsburgh Steelers vs Baltimore Ravens live stream, NFL, week 1, 11.09.2011  Live TV Streams Discussions