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!

Member Avatar for iamthwee

That's wierd I can't see my reply he he, oh well.

Please can you post a sample of what your infile looks like.

Sure thing, I was thinking I should have done that.

0845 34.2 32.2 101.5
0922 38.8 32.2 112.1
1047 44.8 32.4 142.5
1159 51.3 32.0 152.0
1215 55.5 31.8 166.5
1329 61.3 31.9 186.5
1441 67.5 32.3 208.0
1523 72.1 32.8 226.2
1656 76.8 32.5 256.4
1738 83.5 32.7 286.9
1800 88.9 33.0 318.6

The values from left to right are the military time, temperature, pressure and volume. I am currently just concerned with the temperature, and plan on doing the other two once I manage to get this thing working.

Member Avatar for iamthwee

I would personally read the file in one line at a time and then divide that line into you following attributes, temp, time, pressure etc.

Finding a maximum is simple

int max = 1000000;
int temperature = 0;
if ( currentValue > max )
{
max = currentValue
temperature = currentTemperature
//move to next line
}

Make any sense? Need any code samples?

It seems to make sense but I don't have a chance right now to test it out, gotta run to class.

I'll try it out later tonight, thanks for the help.

Alright, I do understand most of it, I just don't know how to code anything to get the program to read the next line of data.

Just as I thought it was a really easy fix. Got the program running and submitted it for grading.

Thanks for da help thwee.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.