Edit: I have discovered that the errors I receive after the program reads my file are because it is trying to output data that is not there since the file does not read up to the maximum number of records (50). I need to figure out how to make it only output the number of records that were read onto the screen.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
//Defining limit of records
constint recordsLimit = 50;
//Defining new data structure for data entry
struct wave
{
double year;
double month;
double day;
double hour;
double minute;
double waveHeight;
double waveLength;
};
//Defining waveData array to hold 1-dimensional wave struct data
wave waveData[recordsLimit];
//defining data file for wave records using fstream and then opening it for output using function call
ifstream dataFile;
dataFile.open("wave.txt", ios::in);
//Reading file - Includes error if not read
if (!dataFile)
{
cout << "Unable to open file wave.txt";
}
else
{
//ifstream dataFile2;
//dataFile.open("steepWaves.txt", ios::in);
//while (dataFile >> waveData[n].year >> waveData[n].month >> waveData[n].day >> waveData[n].hour >> waveData[n].minute >> waveData[n].waveHeight >> waveData[n].waveLength)
//Outputting info from file onto array
while (! dataFile.eof()) // Read until we hit the end of file
{
for (int n = 0; n <= recordsLimit; n++)
{
dataFile >> waveData[n].year >> waveData[n].month >> waveData[n].day >> waveData[n].hour >> waveData[n].minute >> waveData[n].waveHeight >> waveData[n].waveLength;
}
}
}
//Outputting info from array onto screen
for (int n = 0; n <= recordsLimit; n++)
{
cout << waveData[n].year << waveData[n].month << waveData[n].day << waveData[n].hour << waveData[n].minute << waveData[n].waveHeight << waveData[n].waveLength << endl;
}
dataFile.close();
}
Alright. I implemented that into the reading portion of the file. But for lines 62 to 70. How do I make it so it only prints out the number of records that were read? I have no way of keeping a tab on that. I tried to create a variable such as numberOfRecords but that is inside the for while loop and c++ says it's an uninitialized variable.