Question regarding function/conversion

Hello,
I am almost finished with an assignment and am stuck in one area. The object of the program is to read timestamps and temperatures from an input file, convert the Fahrenheit temps to Celsius, and convert the timestamps to human readable format.

I have written all the necessary functions; the one to convert the timestamp is working well, but I'm having problems converting the temps and checking for invalid input.

The function containing the equation for converting temps from F to C is written correctly (I think) but my call to the function does nothing. Each temp is preceded by a letter F or C indicating the format. I read the F and C characters as chars and the actual temp as a double. I thought I could use an if statement to convert the temp ie if(format == "F") ---> call to conversion function but that doesn't seem to be doing anything at all.

Also, if there are any negative values in the input file, they should be identified, an error message printed to the screen, and the program stopped. However, that is not working either. The message is simply printed next to the incorrect temp.

Hope I explained it correctly...any assistance is very much appreciated! Included is the code I've written so far:

[code]
#include
#include
#include

using namespace std;

void printHeader ();
void convertTimestamp(char[]);
double convertTemp(double, 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;

ifstream infile;

infile.open("biodata.txt"); //opens input file

if(!infile) //verifies that input file opens
{
cout << "Failed to open input file.
";

system("PAUSE");
return 2;
}

printHeader(); //function prints header information

infile >> num_readings;

for(int i = 0; i < num_readings; i++) //reads data into arrays
{
for(int i = 0; i < 8; i++)
{
infile >> date[i];
}
for(int i = 0; i < 4; i++)
{
infile >> time[i];
}
for(int i = 0; i < 1; i++)
{
infile >> format[i];
}

infile >> temp;

if(temp <= 0)
{
cout << "An invalid temperature reading was encountered.";
}
else if(format == "F")
{
ctemp = convertTemp(temp, ctemp);
cout << setprecision(2) << fixed << ctemp;
}
else
cout << temp;

cout << " C --- recorded on ";

convertTimestamp(date); //function converts timestamp to human readable format
cout << " at ";

for(int i = 0; i < 2; i++)
{
cout << time[i];
}
cout << ":";
for(int i = 2; i < 4; i++)
{
cout << time[i];
}

avg_temp = findAvg(temp, ctemp, total_temp, total_ctemp, avg_temp,
num_readings); //function finds average temperature reading

cout << endl;
}

cout << "
Average Temp --- " << setprecision(4) << avg_temp << " C
";

infile.close();

system("PAUSE>NUL");
return 0;
}

//function: printHeader
//parameters: none
//returns: none

void printHeader()
{
cout << "BIODATA Formatted Output:

";
}

//function: convertTemp
//parameters: double temp, double ctemp
//returns: ctemp

double convertTemp(double temp, double ctemp)
{
ctemp = (static_cast<float>(5)/9) * (temp - 32);

return ctemp;
}

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;
}
[/code]

Comments

  • : Hello,
    : I am almost finished with an assignment and am stuck in one area.
    : The object of the program is to read timestamps and temperatures
    : from an input file, convert the Fahrenheit temps to Celsius, and
    : convert the timestamps to human readable format.
    :
    : I have written all the necessary functions; the one to convert the
    : timestamp is working well, but I'm having problems converting the
    : temps and checking for invalid input.
    :
    : The function containing the equation for converting temps from F to
    : C is written correctly (I think) but my call to the function does
    : nothing. Each temp is preceded by a letter F or C indicating the
    : format. I read the F and C characters as chars and the actual temp
    : as a double. I thought I could use an if statement to convert the
    : temp ie if(format == "F") ---> call to conversion function but that
    : doesn't seem to be doing anything at all.
    :

    Either make format a char rather than a char array, then you can check if (format == 'F'). format == "F" actually compiles to: "if (address of format in memory == address of string literal "F" in memory)". Or use the function for string comparisons: strcmp (or stricmp to ignore case).
    In your case, the latter is probably best.

    : Also, if there are any negative values in the input file, they
    : should be identified, an error message printed to the screen, and
    : the program stopped. However, that is not working either. The
    : message is simply printed next to the incorrect temp.
    :

    What it does not is not print the faulty temperature, but the program continues. If that's not what you want, and you want the program to stop, you have to break the for loop (in a proper way!).
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion