#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
// Declare array size
constint ARRAY_SIZE = 1000;
// Arrays are declared outside of main, to be used by the entire program
string city[ARRAY_SIZE];
double lowTemp[ARRAY_SIZE];
double highTemp[ARRAY_SIZE];
int loadData(int sz);
void showAll (int count);
void main()
{
cout << loadData(ARRAY_SIZE);
showAll(loadData(ARRAY_SIZE));
system ("pause.");
}
// This function reads the data and stores it in the parallel arrays
int loadData(int sz)
{
cout << "Please enter the file path name: " << endl;
ifstream inFile;
string path;
cin >> path;
string inFileName = path;
inFile.open(inFileName.c_str());
int count = 0;
while (!inFile.eof())
{
for (; count < sz; count ++)
{
inFile >> city[count] >> lowTemp[count] >> highTemp[count];
}
}
return count;
// Close the two files
inFile.close();
// This next line seems to be required to put the file
// in a state where it can be opened again. May also
// have to do for outFile.
inFile.clear(std::ios_base::goodbit);
}
void showAll(int count)
{
for(int i = 0; i <= count; i++)
cout << city[i] << "(" << lowTemp[i] << ", " << highTemp[i] << ")" << endl;
}
system ("pause."); = bad
This is c++ so use cin.get() ;)
You're doing it backwards. Try:
1 2 3 4 5 6
ofstream outFile;
outFile.open("my file.txt"); //notice the .txt
outFile << "this will be printed to file" << endl;
outFile.close();
Think of it as the familiar 'cout' which prints things out to screen. outFile prints things out to the specified file.
Note that the member function .clear() is not needed in your case. This is used to reset the bits if you made your ofstream object act like an ifstream (or vice versa).