Menu

Question regarding character arrays

JennH
2008-01-29
2012-09-26
  • JennH

    JennH - 2008-01-29

    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];

    ifstream infile;
    
    infile.open(&quot;biodata.txt&quot;);
    
    if(!infile)
    {
      cout &lt;&lt; &quot;Failed to open input file.\n&quot;;
    
      system(&quot;PAUSE&quot;);
      return 2;
    }
    
    infile &gt;&gt; num_readings;
    
    for(int i = 0; i &lt; num_readings; i++)
    {
        for(int j = 0; j &lt; 8; j++)
        {
           infile &gt;&gt; date[j];
        }                                
        for(int k = 0; k &lt; 4; k++)
        {
           infile &gt;&gt; time[k];
        }   
        for(int l = 0; l &lt; 6; l++)
        {  
           infile &gt;&gt; temp[l];
        }     
    }
    
    printHeader();
    
    for(int i = 0; i &lt; 8; i++)     
       cout &lt;&lt; date[i];
    for(int i = 0; i &lt; 4; i++)
       cout &lt;&lt; time[i];
    for(int i = 0; i &lt; 6; i++)
       cout &lt;&lt; temp[i];
    
    infile.close();
    
    system(&quot;PAUSE&gt;NUL&quot;);
    return 0;
    

    }

    //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);

     return ctemp;
    

    }

     
    • cpns

      cpns - 2008-01-30

      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.

       
    • Musa

      Musa - 2008-01-30

      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]

       
      • cpns

        cpns - 2008-01-30

        Those declarations are not valid in C++ or even C!

         
    • cpns

      cpns - 2008-01-30

      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

       
    • JennH

      JennH - 2008-01-30

      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?

       
      • cpns

        cpns - 2008-01-31

        >> 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

         
    • JennH

      JennH - 2008-01-31

      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;

      ifstream infile;
      
      infile.open(&quot;biodata.txt&quot;);  //opens input file
      
      if(!infile)  //verifies that input file opens
      {
        cout &lt;&lt; &quot;Failed to open input file.\n&quot;;
      
        system(&quot;PAUSE&quot;);
        return 2;
      }
      
      printHeader();  //function prints header information
      
      infile &gt;&gt; num_readings;
      
      for(int i = 0; i &lt; num_readings; i++)  //reads data into arrays
      {
          for(int i = 0; i &lt; 8; i++)
          {
             infile &gt;&gt; date[i];
          }                                
          for(int i = 0; i &lt; 4; i++)
          {
             infile &gt;&gt; time[i];
          }   
          for(int i = 0; i &lt; 1; i++)
          {  
             infile &gt;&gt; format[i];
          }
      
          infile &gt;&gt; temp;
      
          if(temp &lt;= 0)
          {
             cout &lt;&lt; &quot;An invalid temperature reading was encountered.&quot;;
          }               
          else if(format !=&quot;C&quot;)
          {
             temp = convertTemp(temp);  
             cout &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; temp;
          }
          else
             cout &lt;&lt; temp;
      
          cout &lt;&lt; &quot; C --- recorded on &quot;;
      
          convertTimestamp(date);   //function converts timestamp to human readable format
          cout &lt;&lt; &quot; at &quot;;
      
          for(int i = 0; i &lt; 2; i++)
          {
             cout &lt;&lt; time[i];
          }   
          cout &lt;&lt; &quot;:&quot;;
          for(int i = 2; i &lt; 4; i++)
          {
             cout &lt;&lt; time[i];
          }
      
          avg_temp = findAvg(temp, ctemp, total_temp, total_ctemp, avg_temp, 
          num_readings);  //function finds average temperature reading
      
          cout &lt;&lt; endl;
      }
      
      cout &lt;&lt; &quot;\nAverage Temp --- &quot; &lt;&lt; setprecision(4) &lt;&lt; avg_temp &lt;&lt; &quot; C\n&quot;;
      
      infile.close();
      
      system(&quot;PAUSE&gt;NUL&quot;);
      return 0;
      

      }

      //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);

       return temp;
      

      }

      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;
      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.