Hello out there,
I am trying to compile some simple code but am running into a weird error that I can not figure out
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
Aborted
This is happening when I do an Infile>> read in the code below. What I have done is commented out the read piece by piece to see what variable is causing the issue, and it happens when I start reading in the float variables d1-d10. Any clues?
Thanks in advance
/***************************************
Devang N. Joshi
Homework One
CSCI 325 - Averaging in C++
January 19th, 2011
The purpose of this program is to serve
as basic review on simple file I/O in
C++ using a well structured data file
and performing simple math on it.
***************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
//declare all variables
ifstream Infile;
ofstream Outfile;
int stationNum = 0; //variable for the station number
char dateCode[13]; //variable for the date code
char dataType[3]; /*variable that shows what "type" the data is
example...pwl stands for "primary water level"*/
float d1=0;
float d2=0;
float d3=0;
float d4=0;
float d5=0;
float d6=0;
float d7=0;
float d8=0;
float d9=0;
float d10=0;
//ten variables for water levels over an hour
double daySum=0; //sum of all the water levels over 24hrs
double dayAvg=0; //average water level for a given day
double denominator=240;
//240 data points per day, used to cal dayAvg
int rowCount=0; //counter used to keep track of day change
//open and test files for input & output
Infile.open("lab1.txt",ios::in);
if(!Infile)
{
cerr<<"Error with input file....terminating program";
exit(1);
}
Outfile.open("lab1_out.txt",ios::out);
if(Outfile.fail())
{
cerr<<"Error with output file....terminating program";
exit(1);
}
//initial write to output file...write headings to output file
Outfile<<"Data"<<endl<<endl;
Outfile<<"Date"<<" "<<"Average Water Height"<<endl;
Outfile<<"-----------------------------"<<endl<<endl;
/*
Logic to calculate average daily water levels and
print them to the output files
*/
/*while(!Infile.eof()) //test against end of file marker
{*/
if(rowCount<24) //at 24 one day has elapsed
{
Infile>>stationNum>>dateCode>>dataType>>d1>>d2>>d3>>d4>>d5>>d6>>d7>>d8>>d9>>d10;
//daySum=daySum+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10;
rowCount++;
}
/*else //picked up when one days worth of data is read in, can be averaged now
{
dayAvg=daySum/denominator; //calculate the average
Outfile<<dateCode<<" "<<dayAvg<<endl; //print data to the output file
daySum=0; //reset daysum
dayAvg=0; //reset dayavg
rowCount=0; //reset rowcount
}//if-else*/
//}//while
//final write to output file to mark end of data
Outfile<<endl<<endl;
Outfile<<"End of data"<<endl;
//close input & output files
Infile.close();
Outfile.close();
return 0;
}//main