I am writing a program for class that I keep getting a segmentation fault on. I am fairly certain that it has to do with the assignment of the 'string location.' Could anyone possibly give me a hint as to what I'm doing wrong? I'm very new to this, I've tried everything I can think of. Thanks!

#include <string>
#include <iostream>
using namespace std;



class trip
{
	private:
		int distance;
		int selection;
		int milesPerHour;
		float time;
	 	string location;
	public:
		trip(){ distance = 0; selection = 0; milesPerHour = 0; time = 0; location = "";}
		trip Init( int );
		//void setTrip( int );
		void setMPH( int );
		int getMPH();
		//int getTrip();
		//void Print();
};


trip trip::Init( int choice )
{
	cout << "in Init." << endl;
	selection = choice;
	cout << "selection = " << selection << endl;
	if ( selection == 1 ) 
	{
		location = "Jackson, WY";//PRETTY SURE PROBLEM IS HERE
		distance = 1391;
	}
	cout <<  "  " << distance <<endl;
	cout << "After 'if' statement." << endl;	
	

}


/*void trip::setTrip( int n )
{
	selection = n;
}*/

void trip::setMPH( int n )
{
	milesPerHour = n;
}

int trip::getMPH()
{
	return milesPerHour;
}

/*int trip::getTrip()
{
	return selection;
}*/
/*void trip::Print()
{
	cout << "  " << milesPerHour;
}*/


main()
{
	trip userTrip;
	
	int choice, mph;
	choice = mph = 0;
	
	cout << endl << "---- Choose a trip from the list ----"<<endl << endl;
	cout << "1.  Jackson, WY"<< endl;
	cout << "2.  South Royalton, VT" << endl;
	cout << "3.  Rochester, NY" << endl;
	cout << "4.  Portland, OR" << endl;
	cout << "5.  Wooster, OH" << endl;
	cout << "6.  Branson, MO" << endl;
	cout << "7.  Trego, WI" << endl;
	cout << "8.  Marquette, MI" << endl;
	cout << "9.  Gatlinburg, TN" << endl;
	cout << "10. Fairbanks, AK" << endl << endl;
	cin >> choice;
	cout << "How fast will you drive?" << endl;
	cin >> mph;
	userTrip.setMPH( mph );
	//Print();

	userTrip.Init( choice );
	cout << "Back in main()" << endl;
}

Recommended Answers

All 3 Replies

You don't actually return anything from Init(), make it void.

Thank you! I had a feeling it was going to be something simple I messed up on. Thanks for taking the time to look at it.

No prob. It often is like that so no worries.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.