I have an assignment ... but it's really unnecessary to post all the details up. Basically, I have a header file and an implementation file (and I'm supposed to include an application file, but I don't have the slightest clue by what the professor means by that?).

I have the program working using the following test data:
Airline: DL
Flight Number: 478
From: SF
To: DFW
Departure: Jul 2,2008 10:25 AM
Arrival: Jul 2,2008 2:45 PM

Except for one problem... After I enter all the information, the info is spit back out to the screen, but the departure line is skipped and instead it moves to the arrival line, and arrival info never shows. Please help... I know it has to be something simple that I'm just overlooking.

Here's the code I have so far:

File: assign_3_header.h

#include <string>
using namespace std;

// Creating Flight class - no pun intended.
class Flight
{
	string air_;
	int fli_;
	string ori_;
	string des_;
	string dep_;
	string arr_;

public:
	string airline;
	int flightNumber;
	string origin;
	string destination;
	string departureTime;
	string arrivalTime;

	// Default constructor -- Assigns empty strings to all string data members
	// and 0 to the numeric member variables
	Flight()
	{
		airline = "";
		flightNumber = 0;
		origin = "";
		destination = "";
		departureTime = "";
		arrivalTime = "";
	}

	void setAirline(string s) {air_ = s;};
	void setFlightNumber(int y) {fli_ = y;};
	void setOrigin(string o) {ori_ = o;};
	void setDestination(string x) {des_ = x;};
	void setDeparture(string p) {dep_ = p;};
	void setArrivalTime(string z) {arr_ = z;};

	string getAirline()
	{ return air_; }
	int getFlightNumber()
	{ return fli_; }
	string getOrigin()
	{ return ori_; }
	string getDestination()
	{ return des_; }
	string getDeparture()
	{ return dep_; }
	string getArrivalTime()
	{ return arr_; }

	
	// Constructor that accepts values as arguments and assigns them to the
	// data member variables
	Flight (string a, int b, string c, string d, string e, string f)
	{
		airline = a;
		flightNumber = b;
		origin = c;
		destination = d;
		departureTime = e;
		arrivalTime = f;
	}
	
};

and

File: assign_3_source.cpp

#include "assign_3_header.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	Flight flight1; // flight object 1
	Flight flight2; // flight object 2
	Flight flight3; // flight object 3
	string a_n; // local variable for air_
	int f_n; // local variable for fli_
	string o_n; // local variable for ori_
	string d_n; // local variable for des_
	string d_t; // local variable for dep_
	string a_t; // local variable for arr_

	cout << "Please enter your airline name: ";
	cin >> a_n;
	flight1.setAirline(a_n);
	cout << "Please enter your flight number: ";
	cin >> f_n;
	flight1.setFlightNumber(f_n);
	cout << "Please enter your origin: ";
	cin >> o_n;
	flight1.setOrigin(o_n);
	cout << "Please enter your destination: ";
	cin >> d_n;
	flight1.setDestination(d_n);
	cout << "Please enter your departure information: ";
	std::getline(std::cin, d_t ,'\n');
	flight1.setDeparture(d_t);
	cout << "Please enter your arrival information: ";
	std::getline(std::cin, a_t, '\n');
	flight1.setArrivalTime(a_t);

	cout << endl;
	cout << "Airline: " << flight1.getAirline() << endl;
	cout << "Flight Number: " << flight1.getFlightNumber() << endl;
	cout << "From: " << flight1.getOrigin() << endl;
	cout << "To: " << flight1.getDestination() << endl;
	cout << "Departure: " << flight1.getDeparture() << endl;
	cout << "Arrival: " << flight1.getArrivalTime() << endl;

	return 0;
}

Please excuse my lack of comments. I tend to add them after the fact, so if you have any questions about the code, don't hesitate to ask.

Recommended Answers

All 6 Replies

Any kind of feedback would help. Has anyone else replicated my problem?

>>and I'm supposed to include an application file, but I don't have the slightest clue by what the professor means by that
The application file is a *.cpp file that contains main(). I would assume from your comment that main() should not be in the same file as the class implementation. So your project would consist of a header file and at least two *.cpp files.

Appears you are mixing up the variables in lines 7-12 of the header file and lines 15-20. Just delete one of the two sets of variables so that it isn't so confusing.

Thanks for that info. I'll make sure I have a working program before I split off into another cpp file.

The variables aren't mixed up, but I commented the first set out and changed what I had listed to this:

assign_3_header.h

#include <string>
using namespace std;

// Creating Flight class - no pun intended.
class Flight
{
	/*string air_;
	int fli_;
	string ori_;
	string des_;
	string dep_;
	string arr_;*/

public:
	string airline;
	int flightNumber;
	string origin;
	string destination;
	string departureTime;
	string arrivalTime;

	// Default constructor -- Assigns empty strings to all string data members
	// and 0 to the numeric member variables
	Flight()
	{
		airline = "";
		flightNumber = 0;
		origin = "";
		destination = "";
		departureTime = "";
		arrivalTime = "";
	}

	void setAirline(string s) {airline = s;};
	void setFlightNumber(int y) {flightNumber = y;};
	void setOrigin(string o) {origin = o;};
	void setDestination(string x) {destination = x;};
	void setDeparture(string p) {departureTime = p;};
	void setArrivalTime(string z) {arrivalTime = z;};

	string getAirline()
	{ return airline; }
	int getFlightNumber()
	{ return flightNumber; }
	string getOrigin()
	{ return origin; }
	string getDestination()
	{ return destination; }
	string getDeparture()
	{ return departureTime; }
	string getArrivalTime()
	{ return arrivalTime; }

	
	// Constructor that accepts values as arguments and assigns them to the
	// data member variables
	Flight (string a, int b, string c, string d, string e, string f)
	{
		airline = a;
		flightNumber = b;
		origin = c;
		destination = d;
		departureTime = e;
		arrivalTime = f;
	}
	
};

and

#include "assign_3_header.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	Flight flight1; // flight object 1
	Flight flight2; // flight object 2
	Flight flight3; // flight object 3
	string a_n; // local variable for air_
	int f_n; // local variable for fli_
	string o_n; // local variable for ori_
	string d_n; // local variable for des_
	string d_t; // local variable for dep_
	string a_t; // local variable for arr_

	// Asking for user input for flight1
	cout << "Please enter your airline name: ";
	cin >> a_n;
	flight1.setAirline(a_n);
	cout << "Please enter your flight number: ";
	cin >> f_n;
	flight1.setFlightNumber(f_n);
	cout << "Please enter your origin: ";
	cin >> o_n;
	flight1.setOrigin(o_n);
	cout << "Please enter your destination: ";
	cin >> d_n;
	flight1.setDestination(d_n);
	cout << "Please enter your departure information: ";
	getline( cin, d_t );
	//std::getline(std::cin, d_t ,'\n');
	flight1.setDeparture(d_t);
	cout << "Please enter your arrival information: ";
	getline( cin, a_t );
	//std::getline(std::cin, a_t, '\n');
	flight1.setArrivalTime(a_t);
	
	// Displaying flight1 information
	cout << endl;
	cout << "Airline: " << flight1.getAirline() << endl;
	cout << "Flight Number: " << flight1.getFlightNumber() << endl;
	cout << "From: " << flight1.getOrigin() << endl;
	cout << "To: " << flight1.getDestination() << endl;
	cout << "Departure: " << flight1.getDeparture() << endl;
	cout << "Arrival: " << flight1.getArrivalTime() << endl;
	
	return 0;
}

BUT more of the same. I also updated the cpp file I had.

Instead of showing up in console as:

Airline: DL
Flight Number: 478
From: SF
To: DFW
Departure: Jul 2,2008 10:25 AM
Arrival: Jul 2,2008 2:45 PM

Where I (the user) entered, DL, 478, SF... etc.

...it shows up as:

Airline: DL
Flight Number: 478
From: SF
To: DFW
Departure:
Arrival: Jul 2,2008 10:25 AM

by making one small change I get this:

Please enter your airline name: stl
Please enter your flight number: 100
Please enter your origin: stl
Please enter your destination: mo
Please enter your departure information: adf
Please enter your arrival information: lkj

Airline: stl
Flight Number: 100
From: stl
To: mo
Departure: adf
Arrival: lkj
Press any key to continue . . .

I added cin.ignore() after line 29 in the *.cpp file.

I played with it and ended up putting it after Line 30 and it worked... but with the user having to hit an extra ENTER after entering the departure info.

It works... I'll just figure out how to fix that small bug now.

Thanks!

Its not a bug in your program -- cin >> operator leaves the '\n' in the keyboard buffer and the following getline() stops at the '\n'. So you have to remove the '\n' after the >> operator in order to make getline() work correctly.

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.