I currently have this code and all of the formatting is correct... I just can't seem to get it to create rows in the output text file... How would I do this? I've tried to do a for loop in the function and in the main() but it seems to not work if I do that so I am very confused right now.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void output(string flightnumber, int arrival1, int arrival2, int realarrival1, int realarrival2, char dummy1, char dummy2)
{
ofstream flight;
flight.open("flightdata.dat");
if (flight.fail())
{
cout << "Error..." << endl;
exit(1);
}
cout << "Enter the flight number: ";
cin >> flightnumber;
if (flightnumber == "end")
{
exit(1);
}
flight << flightnumber << setw(4);
cout << "Enter the scheduled/actual arrival times (hh:mm hh:mm):";
cin >> arrival1 >> dummy1 >> arrival2 >> realarrival1 >> dummy2 >> realarrival2;
flight << arrival1 << dummy1 << arrival2 << setw(4) << realarrival1 << dummy2 << realarrival2;
flight << ('\n');
}
int main()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
output(flightnumber, arrival1, arrival2, realarrival1, realarrival2, dummy1, dummy2);
return 0;
}
You are using uninitialized variables in main
. They serve no purpose in there anyway. Remove variable declarations from main
and put them in output
:
void output()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
ofstream flight;
flight.open("c:\\test\\___flightdata.txt", ios::app);
...
}
int main()
{
output();
return 0;
}
You may want to add ios::app
flag as pointed out in the other answer.