Help with a seemingly simple program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 7
Reputation: Schmitty27 is an unknown quantity at this point 
Solved Threads: 0
Schmitty27 Schmitty27 is offline Offline
Newbie Poster

Help with a seemingly simple program

 
0
  #1
Nov 1st, 2006
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?

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. bool convert(int , int& , int&);
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. ifstream infile( "hw3_input.txt" );
  12. ofstream outfile( "hw3_output.out" );
  13.  
  14. int time;
  15. int mil_time; // Given military time
  16. int hour;
  17. int minute;
  18. int temperature;
  19. int MaxTimeTemp;
  20. int MaxTemp;
  21.  
  22.  
  23. while (!infile.fail()){
  24.  
  25. infile >> mil_time;
  26. time = mil_time;
  27. MaxTimeTemp = time;
  28.  
  29. infile >> temperature;
  30. temperature = temperature;
  31. MaxTemp = temperature;
  32.  
  33. if (temperature > MaxTemp){
  34. MaxTemp = temperature;
  35. MaxTimeTemp = mil_time;
  36. }
  37.  
  38. }
  39.  
  40.  
  41. bool PM = convert(MaxTimeTemp , hour , minute);
  42.  
  43. if (PM){
  44. cout << "The maximum temperature is " << MaxTemp << " and it occurs at " << hour << ":" << minute << " PM" << endl;
  45. }
  46. else {
  47. cout << "The maximum temperature is " << MaxTemp << " and it occurs at " << hour << ":" << minute << " AM" << endl;
  48. }
  49.  
  50.  
  51.  
  52.  
  53. infile.close();
  54. outfile.close();
  55.  
  56.  
  57.  
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63.  
  64.  
  65. bool convert(int mil_time, int& hour_1 , int& minute_1){
  66.  
  67. hour_1 = mil_time / 100;
  68. minute_1 = mil_time % 100;
  69.  
  70.  
  71. if (hour_1 > 12){
  72. hour_1 = hour_1 - 12;
  73. }
  74. return mil_time >= 1200;
  75.  
  76. }

Thanks for any help!
Last edited by Schmitty27; Nov 1st, 2006 at 4:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #2
Nov 1st, 2006
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.
Last edited by ~s.o.s~; Nov 1st, 2006 at 4:40 pm. Reason: prev reply was blank
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: Schmitty27 is an unknown quantity at this point 
Solved Threads: 0
Schmitty27 Schmitty27 is offline Offline
Newbie Poster

Re: Help with a seemingly simple program

 
0
  #3
Nov 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with a seemingly simple program

 
0
  #4
Nov 1st, 2006
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: Schmitty27 is an unknown quantity at this point 
Solved Threads: 0
Schmitty27 Schmitty27 is offline Offline
Newbie Poster

Re: Help with a seemingly simple program

 
0
  #5
Nov 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: Schmitty27 is an unknown quantity at this point 
Solved Threads: 0
Schmitty27 Schmitty27 is offline Offline
Newbie Poster

Re: Help with a seemingly simple program

 
0
  #6
Nov 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: Schmitty27 is an unknown quantity at this point 
Solved Threads: 0
Schmitty27 Schmitty27 is offline Offline
Newbie Poster

Re: Help with a seemingly simple program

 
0
  #7
Nov 2nd, 2006
Just as I thought it was a really easy fix. Got the program running and submitted it for grading.

Thanks for da help thwee.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC