Hello, I've been working on this problem for a while, its a program that needs to convert military time to civilian time from an input file using a user-defined function. Then the program must find the maximum temperature and list the temperature and the time at which it occurs.
My program runs, but my while loop exits after the first line of data from the infile. Its been driving me crazy, any idea what I'm doing wrong?
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
bool convert(int , int& , int&);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile( "hw3_input.txt" );
ofstream outfile( "hw3_output.out" );
int time;
int mil_time; // Given military time
int hour;
int minute;
int temperature;
int MaxTimeTemp;
int MaxTemp;
while (!infile.fail()){
infile >> mil_time;
time = mil_time;
MaxTimeTemp = time;
infile >> temperature;
temperature = temperature;
MaxTemp = temperature;
if (temperature > MaxTemp){
MaxTemp = temperature;
MaxTimeTemp = mil_time;
}
}
bool PM = convert(MaxTimeTemp , hour , minute);
if (PM){
cout << "The maximum temperature is " << MaxTemp << " and it occurs at " << hour << ":" << minute << " PM" << endl;
}
else {
cout << "The maximum temperature is " << MaxTemp << " and it occurs at " << hour << ":" << minute << " AM" << endl;
}
infile.close();
outfile.close();
return 0;
}
bool convert(int mil_time, int& hour_1 , int& minute_1){
hour_1 = mil_time / 100;
minute_1 = mil_time % 100;
if (hour_1 > 12){
hour_1 = hour_1 - 12;
}
return mil_time >= 1200;
}
Thanks for any help!