Hello,
I am working on an assignment for school and am not sure how to get past this problem. Part of the assignment involves converting a timestamp read from an input file (in YYYYMMDDHHMM form for date/time ie. 200701250830) to human readable format (MM/DD/YYYY HH:MM). Each record in the file is the timestamp followed by a temperature (ex. F35.56) in either Fahrenheit or Celsius with a space in between. Celsius is to be converted to Fahrenheit. The first record is merely an integer denoting the number of records in the file. Our instructor suggested we use character arrays to input the time/date/temp data from the file. I followed his guidelines for the code to read the information into the arrays (one for date, one for time, and one for temperature) and decided to test the output to make sure that the input values were correct. When I run the program and output the values contained in the arrays, only the last set of records appears (there should be two lines of time/date/temp readings), the first value for the date is added to the end of the output, and one number in the temperature is dropped entirely. I'm guessing this has something to do with the space between the timestamp and the temp but we're supposed to leave the records in that format. I'm not sure why only one record prints instead of two. And as for the timestamp conversion - would I need to accomplish that by somehow copying values from the array containing the timestamp to a new array which would include the proper formatting and sort them? I was thinking of reading the temperature as a double instead of a char array since the sizes do vary, but then how will the program 'know' which temps are C and need to be converted to F? We covered arrays last semester but the programs were less involved and I didn't have this much trouble. If anyone can tell me what I've done wrong, I would appreciate it! I don't want to appear to be trying to get someone to do my homework FOR me, so if you're only willing to comment on the original question I understand. I've included the code I've come up with so far (I realize the function for temp conversion is incomplete) and the input file contains these records:
2
200707211245 F70.5
200708220812 C19.97
Reading code without formatting is bad enough and can't be helped unfortunately. But faced with a solid block of text with no paragraphs I am going to bed. Maybe tomorrow. Goodnight.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
you are overwriting your data. you have only one variable for date, time and temp but you are using it to store mulitiple sets of data. you could use an array like so
char date[][9];
char time[][5];
char temp[][7];//i dont really know c++ so i dont know if those declarations are allowed but i'm just pointing out 2 dimensional arrays
.
.
.
... date[i][j]
.
.
... time[i][l]
.
.
... temp[i][k]
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Musa is right (although his 'solution' is not helpful).
You iterate through all the records, but only print the data once because it is outside of the loop, you need to output for each record. The solution Musa alluded to - using two dimensional arrays is unnecessary. Print the header onde before the loop, then print the data items at the endo of but inside the loop.
open file
if error stop.
print header
for all items
{
---get item data
---print item data
}
close file
That is structurally how you might code it. The use of plain arrays and character by character output is unusual. You could use nul terminated strings, which would make the output simpler. You might also consider using the C++ string class. But of course you should stick to whatever the assignment is intended to test, the point is to demonstrate understanding of the course material, not 'cleverness'.
I am not sure if that helps, because I have still not read your prose - paragraphs man, paragraphs!
Clifford
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First of all thank you - you're right (obviously) - both about the code and the lack of paragraphs. I'm sorry, I should have been more attentive to that since people are trying to read this stuff!
Jeez, it's always something simple I seem to overlook. The statements printing output are now located where they are supposed to be, and I got the timestamp conversion function working splendidly.
Now my only issue is the temperature conversion. I read the 'F' and 'C' values from the input file as chars, then the actual temperature that follows as a double. I've written the function containing the temperature equation itself and tested it to ensure it works. But calling the function does nothing, all temps remain the same.
The F's and C's are in an array called 'format'. I tried (in pseudocode):
if(format == "C")
call function to convert temp to ctemp
cout << ctemp
else
cout << temp
I'm assuming placement is important in this effort? Or am I just completely off base in what I'm doing? Is there something wrong with the way I'm trying to tell the program to locate the temps it needs to convert?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No, I am obviously the one missing something - a brain perhaps? I mixed up my variables - yes, it should be either of the two you mentioned above. I changed my code to if(format!="C") and all the temperatures were converted this time. I checked and the the 'format' array is recieving the F and C chars.
I didn't want to bother you guys with eons of unformatted code if it wasn't necessary but here it is in case it helps to see the whole thing:
Hello,
I am working on an assignment for school and am not sure how to get past this problem. Part of the assignment involves converting a timestamp read from an input file (in YYYYMMDDHHMM form for date/time ie. 200701250830) to human readable format (MM/DD/YYYY HH:MM). Each record in the file is the timestamp followed by a temperature (ex. F35.56) in either Fahrenheit or Celsius with a space in between. Celsius is to be converted to Fahrenheit. The first record is merely an integer denoting the number of records in the file. Our instructor suggested we use character arrays to input the time/date/temp data from the file. I followed his guidelines for the code to read the information into the arrays (one for date, one for time, and one for temperature) and decided to test the output to make sure that the input values were correct. When I run the program and output the values contained in the arrays, only the last set of records appears (there should be two lines of time/date/temp readings), the first value for the date is added to the end of the output, and one number in the temperature is dropped entirely. I'm guessing this has something to do with the space between the timestamp and the temp but we're supposed to leave the records in that format. I'm not sure why only one record prints instead of two. And as for the timestamp conversion - would I need to accomplish that by somehow copying values from the array containing the timestamp to a new array which would include the proper formatting and sort them? I was thinking of reading the temperature as a double instead of a char array since the sizes do vary, but then how will the program 'know' which temps are C and need to be converted to F? We covered arrays last semester but the programs were less involved and I didn't have this much trouble. If anyone can tell me what I've done wrong, I would appreciate it! I don't want to appear to be trying to get someone to do my homework FOR me, so if you're only willing to comment on the original question I understand. I've included the code I've come up with so far (I realize the function for temp conversion is incomplete) and the input file contains these records:
2
200707211245 F70.5
200708220812 C19.97
include <iostream>
include <fstream>
using namespace std;
void printHeader ();
double convertTemp(double, double);
int main()
{
int num_readings;
char date[9];
char time[5];
char temp[7];
}
//function: printHeader
//parameters: none
//returns: none
void printHeader()
{
cout << "BIODATA Formatted Output:\n\n";
}
//function: convertTemp
//parameters: double ftemp, double ctemp
//returns: ctemp
double convertTemp(double ftemp, double ctemp)
{
ctemp = (static_cast<float>(5)/9) * (ftemp - 32);
}
Reading code without formatting is bad enough and can't be helped unfortunately. But faced with a solid block of text with no paragraphs I am going to bed. Maybe tomorrow. Goodnight.
you are overwriting your data. you have only one variable for date, time and temp but you are using it to store mulitiple sets of data. you could use an array like so
char date[][9];
char time[][5];
char temp[][7];//i dont really know c++ so i dont know if those declarations are allowed but i'm just pointing out 2 dimensional arrays
.
.
.
... date[i][j]
.
.
... time[i][l]
.
.
... temp[i][k]
Those declarations are not valid in C++ or even C!
Musa is right (although his 'solution' is not helpful).
You iterate through all the records, but only print the data once because it is outside of the loop, you need to output for each record. The solution Musa alluded to - using two dimensional arrays is unnecessary. Print the header onde before the loop, then print the data items at the endo of but inside the loop.
open file
if error stop.
print header
for all items
{
---get item data
---print item data
}
close file
That is structurally how you might code it. The use of plain arrays and character by character output is unusual. You could use nul terminated strings, which would make the output simpler. You might also consider using the C++ string class. But of course you should stick to whatever the assignment is intended to test, the point is to demonstrate understanding of the course material, not 'cleverness'.
I am not sure if that helps, because I have still not read your prose - paragraphs man, paragraphs!
Clifford
Clifford
First of all thank you - you're right (obviously) - both about the code and the lack of paragraphs. I'm sorry, I should have been more attentive to that since people are trying to read this stuff!
Jeez, it's always something simple I seem to overlook. The statements printing output are now located where they are supposed to be, and I got the timestamp conversion function working splendidly.
Now my only issue is the temperature conversion. I read the 'F' and 'C' values from the input file as chars, then the actual temperature that follows as a double. I've written the function containing the temperature equation itself and tested it to ensure it works. But calling the function does nothing, all temps remain the same.
The F's and C's are in an array called 'format'. I tried (in pseudocode):
if(format == "C")
call function to convert temp to ctemp
cout << ctemp
else
cout << temp
I'm assuming placement is important in this effort? Or am I just completely off base in what I'm doing? Is there something wrong with the way I'm trying to tell the program to locate the temps it needs to convert?
>> Is there something wrong with the way I'm trying to tell
>> the program to locate the temps it needs to convert?
Impossible to say from the pseudocode. If you have a code error rather than a design error, the pseudocode will not help.
The conversion call I presume something looks like:
ctemp = FtoC( temp ) ;
??
Should that not be
if(format != "C")
or
if(format == "F")
BTW? I am assuming ctemp is in celcius, so there would be no need to convert from temp to ctemp if format were 'C'. Or am I mising something?
Clifford
No, I am obviously the one missing something - a brain perhaps? I mixed up my variables - yes, it should be either of the two you mentioned above. I changed my code to if(format!="C") and all the temperatures were converted this time. I checked and the the 'format' array is recieving the F and C chars.
I didn't want to bother you guys with eons of unformatted code if it wasn't necessary but here it is in case it helps to see the whole thing:
include <iostream>
include <fstream>
include <iomanip>
using namespace std;
void printHeader ();
void convertTimestamp(char[]);
double convertTemp(double);
double findAvg(double, double, double, double, double, int);
int main()
{
int num_readings; //declaration section
char date[9];
char time[5];
char format[2];
double temp;
double ctemp;
double total_temp;
double total_ctemp;
double avg_temp;
}
//function: printHeader
//parameters: none
//returns: none
void printHeader()
{
cout << "BIODATA Formatted Output:\n\n";
}
//function: convertTemp
//parameters: double temp, double ctemp
//returns: ctemp
double convertTemp(double temp)
{
temp = (static_cast<float>(5)/9) * (temp - 32);
}
void convertTimestamp(char date[])
{
for(int i = 4; i < 6 ; i++)
{
cout << date[i];
}
cout << "/";
for(int i = 6; i < 8; i++)
{
cout << date[i];
}
cout << "/";
for(int i = 0; i < 4; i++)
{
cout << date[i];
}
}
//function: findAvg
//parameters: double temp, double ctemp
//returns: avg_temp
double findAvg(double temp, double ctemp, double total_temp, double total_ctemp,
double avg_temp, int num_readings)
{
for(int count = 0; count < num_readings; count++)
total_temp += temp;
total_ctemp += temp;
avg_temp = (total_temp + total_ctemp)/num_readings;
return avg_temp;
}