Hey... So I am having a hard time with a project I have and I would appreciate if someone makes time to read and help me.... It;s kinda big... Anyway, here it is
I am making a program that has two classes. I have 5 locations where I have sensors for temperature, humidity, wind, sunlight. I get their values for every hour for 5 days (600 values). Each location is an instance of a class named node where i store every hour the date and time values as integers and location as string in a struct named 'data'. I have another class, the "main system" from where I call all the functions(e.g getStats, averageTemp, extremeSun e.t.c e.t.c)
I save these struct data every time in a file named "measurements.dat" which I open in binary mode like this
Code:
void Console::saveValuesF(Data valuesC) {
ofstream outData;
if ( !getFileU() ) {
outData.open(DATABASE_FILE, ios::out | ios::trunc | ios::binary);
setFileU(true);
}
else
outData.open(DATABASE_FILE, ios::out | ios::app | ios::binary);
if (outData.is_open()) {
outData.write( (char*)(&valuesC), sizeof(Data) );
outData.close();
}
else
cout<<setw(35)<<"File could not be found"<<endl<<endl;
}
When I call the functions for average temperature and humidity and extreme temperatures I open the file and read it 'data' to 'data' with this
Average Temperature and Humidity
Code:
void Console::averageT_H() {
ifstream inData;
inData.open(DATABASE_FILE, ios::in | ios::binary);
if (inData.is_open()) {
Data curr;
int rodos_tAvrg=0; int rodos_hAvrg=0;
int ioan_tAvrg=0;
int alex_tAvrg=0; int alex_hAvrg=0;
int hania_tAvrg=0; int hania_hAvrg=0;
int kithira_tAvrg=0; int kithira_hAvrg=0;
int j=0;
while ( inData.read( (char*)(&curr), sizeof(Data) ) ) {
j++;
if (curr.location=="Rhodos") {
rodos_tAvrg+=curr.temp;
rodos_hAvrg+=curr.humid;
}
else if (curr.location=="Ioannina") {
ioan_tAvrg+=curr.temp;
}
else if (curr.location=="Alexandroupoli") {
alex_tAvrg+=curr.temp;
alex_hAvrg+=curr.humid;
}
else if (curr.location=="Chania") {
hania_tAvrg+=curr.temp;
hania_hAvrg+=curr.humid;
}
else if (curr.location=="Kithira") {
kithira_tAvrg+=curr.temp;
kithira_hAvrg+=curr.humid;
}
}
cout<<j<<endl;
cout<<endl<<"Average temperature of Rhodos is "<<rodos_tAvrg/(j/5)<<(char)249<<"C"
<<" and average humidity is "<<rodos_hAvrg/(j/5)<<"%"<<endl;
cout<<endl<<"Average temperature of Ioannina is "<<ioan_tAvrg/(j/5)
<<(char)249<<"C"<<endl;
cout<<endl<<"Average temperature of Alexandroupoli is "<<alex_tAvrg/(j/5)<<(char)249
<<"C"<<" and average humidity is "<<alex_hAvrg/(j/5)<<"%"<<endl;
cout<<endl<<"Average temperature of Chania is "<<hania_tAvrg/(j/5)<<(char)249<<"C"
<<" and average humidity is "<<hania_hAvrg/(j/5)<<"%"<<endl;
cout<<endl<<"Average temperature of Kithira is "<<kithira_tAvrg/(j/5)<<(char)249<<"C"
<<" and average humidity is "<<kithira_hAvrg/(j/5)<<"%"<<endl;
}
else
cout<<setw(35)<<"File could not be found"<<endl<<endl;
inData.close();
}
Extreme Temperature in all locations
Code:
void Console::extremeTemp() {
ifstream inData;
Data curr;
Data extreme_high;
Data extreme_low;
inData.open(DATABASE_FILE, ios::in | ios::binary);
if (inData.is_open()) {
inData.read( (char*)(&curr), sizeof(Data) );
extreme_high=extreme_low=curr;
while ( inData.read( (char*)(&curr), sizeof(Data) ) ) {
if ( curr.temp>extreme_high.temp )
extreme_high=curr;
else if ( curr.temp<extreme_low.temp )
extreme_low=curr;
}
cout<<endl<<"Highest temperature was recorded at "<<extreme_high.location<<endl
<<" on "<<setfill('0')<<setw(2)<<extreme_high.day<<"/"<<setw(2)<<extreme_high.month
<<"/"<<setw(4)<<extreme_high.year<<" "<<setw(2)<<extreme_high.hour
<<":00 ---> "<<setfill(' ')<<extreme_high.temp<<endl;
cout<<endl<<"Lowest temperature was recorded at "<<extreme_low.location<<endl
<<" on "<<setfill('0')<<setw(2)<<extreme_low.day<<"/"<<setw(2)<<extreme_low.month
<<"/"<<setw(4)<<extreme_low.year<<" "<<setw(2)<<extreme_low.hour
<<":00 ---> "<<setfill(' ')<<extreme_low.temp<<endl;
}
else
cout<<setw(35)<<"File could not be found"<<endl<<endl;
inData.close();
}
What is my problem? For some reason (I think it may be memory issues, my partner doesnt know) when I call the function void Systemh::avrgTempHum() the first time it works just fine and shows the averages of the 5 nodes but if I call it again the averages temperatures and humidity of the last node are 0 !!!!! Now if I close the program and rerun it with the file with the previus values it shows again the correct values so this means that the 'data' in the file haven't changed right? The other problem is that if run the void Systemh::avrgTempHum() funtion and then the void Systemh::extremeTemp() funtion for extreme temperatures it crashes. This is where i think it may be problems with allocated memory, I dont know, maybe the program tries to get memory that doesnt belong to it but i dont know how to check it and I dont understand. I use a temp variable. Should I perhaps try to work with pointers. Instead of a temp 'data' variable declare a pointer to a 'data' variable and so on, although I am not very sure...
Furthemore if I run the extreme temperature function first and then the average the extreme works just fine but the average has the problem with the last node being 0 and if run then the extreme again it crashes...
I know this is a lot but if anyone can help, it is greatly appreciated. If anyone is interested in the whole program the files can be found here -----> "http://www.geocities.com/vhf_andrew/source.doc" Actually it's a rar file so rename it and extract to a folder. Make a project and add them and then compile....