Help

I need help with this, I am supposed to write flight information to file and then output the information to the console.... having a bit of trouble with this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
 
ofstream myfileOut;  //output file object that is needed to write to a file
ifstream myfileIn;  //input file object needee to read in data from a file

char run, code;
double flightnumber, passengers;
string origin, destination;
 
void writefile();
void readfile();
 
void main()
{
cout << "Airport Flight Schedule" << endl;
cout << "Correct Program?[Y/N]" << endl;
cin >> run;
 
 
writefile();
 
 
    if (run == 'E')
    {
        readfile();
    }
    else if (run == 'N')
    {
        cout << "Goodbye!" << endl;
 
        }
   
 
 
system("PAUSE");
}
 
void writefile()
{
myfileOut.open("airline_data.txt");
while (run == 'Y')
{
        cout << "Enter Flight Number:" << endl;
            cin >> flightnumber;
        cout << "Enter Flight Origin:" << endl;
            getline (cin, origin);
        cout << "Enter Flight Destination:" << endl;
            getline  (cin, destination);
        cout << "Enter Number of Passengers:" << endl;
            cin >> passengers;
            cout << "Enter Flight Code [O/C/D]:" << endl;
                cin >> code;
        cout << "Would you like to enter another flight or end the program?? [Y/E]" << endl;
            cin >> run;
 
    myfileOut << flightnumber << "      " << origin << "        " << destination << "       " << passengers << "        " << " " << code << endl;
 
 
}
myfileOut.close();
}
 
void readfile()
{
myfileIn.open("airline_data.txt");
myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
cout << "Flight No      Origin      Destination     Passengers      Code" << endl;
if(myfileOut.eof())
{
while(getline(myfileIn, origin))
{

    cout << flightnumber << origin << destination << passengers << code;
	myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
}

myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
myfileIn.close();
}
}
Last edited on
anyone?
Hi. You only need 1 or 2 minor changes to compile, link, and run this program.
1) #include <stdlib.h> for the system call
2) declare main as int main() not void main()
3) best to return a value from main() now that it's int main() although
the compiler might let you NOT return a value and still compile.
4) After the above, to get flow correct, move the writefile to an else or at least some sort of better logical flow so that when someone chooses E, it doesn't write the file (and instead calls the method readfile).

Then just type the following at the terminal (I put your program in a file called Austin.cpp so substitute your file for Austin.cpp):
g++ -c Austin.cpp
g++ Austin.o -o Austin

Using g++ automatically includes some stuff that gcc does NOT, making it easier to complile this with g++.

It will then write your file airline_data.txt as follows (output of my sample
run):
456 LAX 4 C
295 ORD 1 D

#include <iostream>
#include <string>
#include <fstream>
// Just need system call header
#include <stdlib.h> // system("PAUSE");
using namespace std;

ofstream myfileOut; //output file object that is needed to write to a file
ifstream myfileIn; //input file object needee to read in data from a file

char run, code;
double flightnumber, passengers;
string origin, destination;

void writefile();
void readfile();

// and change void main() to int main()
int main()
//void main()
{
cout << "Airport Flight Schedule" << endl;
cout << "Correct Program?[Y/N]" << endl;
cin >> run;


if (run == 'E')
{
readfile();
}
else if(run == 'N')
{
cout << "Goodbye!" << endl;
}
else
{
writefile();
}

// system("PAUSE");
// Change system("PAUSE");

// and you should return a value (ideally)
return 0;
}

void writefile()
{
myfileOut.open("airline_data.txt");
while (run == 'Y')
{
cout << "Enter Flight Number:" << endl;
cin >> flightnumber;
cout << "Enter Flight Origin:" << endl;
//getline (cin, origin);
cin >> origin;
cout << "Enter Flight Destination:" << endl;
//getline (cin, destination);
cin >> destination;
cout << "Enter Number of Passengers:" << endl;
cin >> passengers;
cout << "Enter Flight Code [O/C/D]:" << endl;
cin >> code;
cout << "Would you like to enter another flight or end the program??
[Y/E]" << endl;
cin >> run;

myfileOut << flightnumber << " " << origin << " " << destination << " " << passengers << " " << " " << code << endl;


}
myfileOut.close();
}

void readfile()
{
myfileIn.open("airline_data.txt");
myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
cout << "Flight No Origin Destination Passengers Code" << endl;
if(myfileOut.eof())
{
while(getline(myfileIn, origin))
{

cout << flightnumber << origin << destination << passengers << code;
myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
}

myfileIn >> flightnumber >> origin >> destination >> passengers >> code;
myfileIn.close();
}
}

By the way I've already sold this to United, Delta, American, Virgin Atlantic, and Jet Blue, do you want royalties?

I do see a slight problem with program flow on the read side of things. Need to look at this.

You might want to change the if elseif to an if else elseif as follows, after removing the writefile() above the if else else if, in other words move it to the else I show below:

if (run == 'E')
{
readfile();
}
else if(run == 'N')
{
cout << "Goodbye!" << endl;
}
else
{
writefile();
}

Also, you want to open the file for read similar to this:
myfileIn.open("airline_data.txt", ifstream::in);

and change the lines with getline for the origin and desination to just

cin >> origin;
cin >> destination;

And the loop flow of readfile needs to be fixed.
So the writefile and logical flow are ok but the readfile routine needs to be rewritten.
Last edited on
Here is the program, running ok for writing and reading. I did not fix the System call. The readfile method is actually very simple. You had been reading into origin, you just need to declare a string line. And you needed
if(!myfileIn.eof()) rather than if(myfileIn.eof()). And you had used myfileOut in the above line rather than myfileIn.

Sorry, my formatting in my editor (which is Geany) doesn't copy over well, I need to look into this.

#include <iostream>
#include <string>
#include <fstream>
// Just need system call header
#include <stdlib.h> // system("PAUSE");
using namespace std;

ofstream myfileOut; //output file object that is needed to write to a file
ifstream myfileIn; //input file object needee to read in data from a file

char run, code;
double flightnumber, passengers;
string origin, destination;

void writefile();
void readfile();

// and change void main() to int main()
int main()
//void main()
{
cout << "Airport Flight Schedule" << endl;
cout << "Correct Program?[Y/N]" << endl;
cin >> run;

if (run == 'E')
{
readfile();
}
else if(run == 'N')
{
cout << "Goodbye!" << endl;
}
else
{
writefile();
}



//system("PAUSE");

// and you should return a value (ideally)
return 0;
}

void writefile()
{
myfileOut.open("airline_data.txt");
while (run == 'Y')
{
cout << "Enter Flight Number:" << endl;
cin >> flightnumber;
cout << "Enter Flight Origin:" << endl;
//getline (cin, origin);
cin >> origin;
cout << "Enter Flight Destination:" << endl;
//getline (cin, destination);
cin >> destination;
cout << "Enter Number of Passengers:" << endl;
cin >> passengers;
cout << "Enter Flight Code [O/C/D]:" << endl;
cin >> code;
cout << "Would you like to enter another flight or end the program?? [Y/E]" << endl;
cin >> run;

myfileOut << flightnumber << " " << origin << " " << destination << " " << passengers << " " << " " << code << endl;


}
myfileOut.close();
}

void readfile()
{
string line;
myfileIn.open("airline_data.txt", ifstream::in);
cout << "Flight No Origin Destination Passengers Code" << endl;
if(!myfileIn.eof())
{
while(getline(myfileIn, line))
{
cout << line << endl;
}

myfileIn.close();
}
}
Last edited on
Thank you so much my man @CPPAWhile
CPPAWhile wrote:
3) best to return a value from main() now that it's int main() although
the compiler might let you NOT return a value and still compile.

Then just type the following at the terminal (I put your program in a file called Austin.cpp so substitute your file for Austin.cpp):
g++ -c Austin.cpp
g++ Austin.o -o Austin

Using g++ automatically includes some stuff that gcc does NOT, making it easier to complile this with g++.

And you needed
if(!myfileIn.eof()) rather than if(myfileIn.eof()).


A return value is not required by the C++ standard for the main function, although one may still provide one if they wish.

Compile with warnings on, and specify the latest standard that your compiler version will support:

g++ -std=c++17 - Wall -Wextra -pedantic-errors Austin.cpp -o Austin.exe

The above assume windows (the exe output), and c++17 which is current, but one should have at least c++14 by now. C++14 is the default standard for compilation for g++ these days, meaning that if nothing is specified it will compile to the c++14 standard.

There is no need to compile in two stages for this small program, the above will do it all in one.

Compile c++ programs with g++, gcc will not work for this, it will choke on any c++ construct. gcc is for c programs.

if(!myfileIn.eof()) is not needed at all and is wrong. the while(getline(myfileIn, line)) takes care of the end of file.

There are much better ways of keeping the window open, than using a system call. See the post stickied to the beginners board.

Always use code tags when posting code.
I just made a few changes to the program as it was getting sloppy:

- removed unnecessary includes, comments, and code
- changed the ofstream write to append since it truncates by default
- formatted the columns a little better although they will only look nice for 3-letter flight numbers (this would take more formatting on your part)
- replaced the system("PAUSE") with a simple way to gobble up one enter char to pause the program, and also allow exiting the program more gracefully, and without using some complicated method.
- I added some text to guide the user a little more (as much for my own benefit as yours)
- changed the readfile per TheIdeaMan's suggestions
- used code tags per TheIdeaMan's suggestions - my tabs are still a bit troublesome, sorry about that.
- added if(run != 'Y') section for invalid keystrokes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
 
ofstream myfileOut;  //output file object that is needed to write to a file
ifstream myfileIn;  //input file object needee to read in data from a file

char run, code;
double flightnumber, passengers;
string origin, destination;
 
void writefile();
void readfile();
void pause();

int main() 
{
	cout << "Airport Flight Schedule" << endl;
	cout << "Correct Program?[Y (Enter flights)/N (Exit program) /(E to dump flights)]" << endl;
	cin >> run;
 
	if (run == 'E')
	{
		readfile();
	}
	else if(run == 'N')
	{
		cout << "Goodbye!" << endl;
	}
	else
	{
		if(run != 'Y')
		{
			cout << "Response = " << run << " is invalid." << endl;
			cout << "Please choose 'Y' to write routes, 'N' to exit, or 'E' to read routes." << endl;
			return -1;
		}
	writefile();
	}
   

	cout << "Press enter (or Ctrl-C) to quit ... " << endl;
	cin.ignore();
	cin.get();
	return 0;
}
 
void writefile()
{
	myfileOut.open("airline_data.txt", std::ofstream::out | std::ofstream::app);
	while (run == 'Y')
	{
		cout << "Enter Flight Number:" << endl;
			cin >> flightnumber;
		cout << "Enter Flight Origin:" << endl;
			cin >> origin;
		cout << "Enter Flight Destination:" << endl;
			cin >> destination;
		cout << "Enter Number of Passengers:" << endl;
			cin >> passengers;
		cout << "Enter Flight Code [O/C/D]:" << endl;
			cin >> code;
		cout << "Would you like to enter another flight or end the program?? [Y/E]" << endl;
			cin >> run;
 
		myfileOut << flightnumber << "            " << origin << "         " << destination << "             " << passengers << "               " << code << endl;
	}
	
	myfileOut.close();
}
 
void readfile()
{
	string line;
	
	myfileIn.open("airline_data.txt", ifstream::in);
	cout << "Flight No      Origin      Destination     Passengers      Code" << endl;
	while(getline(myfileIn, line))
	{
		cout << line << endl;
	}

	myfileIn.close();
}

Last edited on
@CPPAWhile , austin9398

Some other things to do, maybe the apply more to the OP:

Get rid of using namespace std; it will bite you one day. Learn about what namespaces are, Google the topic. Believe me when I say the easiest thing to do is put std:: before each std thing, that's what the expert coders here all do.

Get rid of global variables, they are bad form also. Another thing that will bite one day.

Make use of the toupper function on the run variable, so the user doesn't wonder why the program refuses to run when they enter a lowercase value.

Line 37 is a bit drastic, give the user a chance to have another go - use a loop.

When opening files, check to see if it worked.

The writefile and readfile functions could potentially return bool to signal that have completed successfully.

Is there a reason why flight number and passengers are of type double? The flight should be a std::string, and PasengersOnBoard could be std::size_t or at least an unsigned int

std::size_t is the largest unsigned integer type the system has, and is used in the STL for the sizes of anything. My main point thought is the type should be unsigned, we don't want negative values there, so use the type system to help you.

Good Luck !!
Last edited on
Topic archived. No new replies allowed.