3 Replies - 476 Views - Last Post: 10 November 2012 - 06:57 AM Rate Topic: -----

#1 karatekungfuchicken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-September 12

Output will not come out correctly

Posted 09 November 2012 - 06:54 PM

The project is to display a binary text file, and calculate temperature Information. I don't see where the problem is coming from. I have no problem opening the file, but it appears as though its still outputting binary (possibly) not sure though. The binary.txt file is attached.

This is a sample of what the output should be:

City Low Temperature High Temperature
------------------------------------------------------
Port Charles 3 99
Springfield -19 102
Llanview 12 108
Salem 46 114




#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

struct cityInfo
{
    char cityName[25];
    int lowTemp;
    int highTemp;
};

int const MAXCHAR = 30;

int buildArray( cityInfo [] );
void printArray ( cityInfo [], int );
void sortArray ( cityInfo [], int );
void calcStats ( cityInfo [], int, double&, double&, double&, double& );


//Begin Main Function

int main ()
{
    int numCities = 0;
    double averageLow;
    double averageHigh;
    double stdDevLow;
    double stdDevHigh;
    
    cityInfo cityArray[ MAXCHAR ];
    
    int n = buildArray( cityArray );
    
    cout << "Unsorted City Reports: " << endl;
    
    printArray( cityArray, n );
    
    sortArray( cityArray, n );
    
    cout << "Sorted City Reports:" << endl;
    
    printArray( cityArray, n );
    
    calcStats( cityArray, numCities, averageLow, averageHigh, stdDevLow, stdDevHigh );
    
    cout << endl << "Temperature Statistics: " << endl;
    cout << "---------------------------" << endl;
    cout << "Average Temperature - Low: " << setw(15) << averageLow << endl;
    cout << "Average Temperature - High: " << setw(15) << averageHigh << endl;
    cout << endl << "Standard Deviation - Low Temperatures: " << setw(15) << stdDevLow << endl;
    cout << "Standard Deviation - High Temperatures: " << setw(15) << stdDevHigh << endl;
    cout << endl;
    
    return 0;
}

//The functions to be called


int buildArray( cityInfo cityArray[] )
{
    ifstream inFile;
    char cityName[25];
    int highTemp;
    int lowTemp;
    int numCities = 0;
    int i;
    
    inFile.open( "binary_cities.txt", ios::binary );
    
    if ( inFile.fail() )
        {
        cout << "The binary_cities.txt input file did not open";
        }
    
    cityInfo city;
    
    inFile.read( ( char * ) &city, sizeof ( cityInfo ) );
	
    while ( inFile )
    	{
		cityArray [ i ] = city; 
		i++; 
		inFile.read ( ( char* ) &city , sizeof ( cityInfo ) );
    	}
    
	inFile.close();
    
    return i;  
}

void printArray( cityInfo cityArray[], int numCities )
{
	cout << "City\t\t\tLow Temperature\tHigh Temperature" << endl;
	cout << "---------------------------------------" << endl;

	for( int i = 0; i < numCities; i++ )
		{
		cout << left << setw( 25 ) << cityArray[ i ].cityName << right << setw( 15 ) << cityArray[ i ].lowTemp << setw( 10 ) << cityArray[ i ].highTemp << endl;
		}
		
	cout << "---------------------------------------" << endl;
}



void sortArray ( cityInfo cityArray[], int numCities )
{
    cityInfo temperature;
    
    for( int i = 0; i < numCities - 1; i++ )
        {
        for( int n = i + 1; n < numCities; n++ )
            {
            char city1 = cityArray[i].cityName[0];
			char city2 = cityArray[n].cityName[0];
            
            if( city1 > city2 )
                {
                temperature = cityArray[i];
				cityArray[i] = cityArray[n];
				cityArray[n] = temperature;
                }
            }
        }
}


void calcStats ( cityInfo cityArray[], int numCities, double &averageLow, double &averageHigh, double &stdDevLow, double &stdDevHigh )
{
    int lowSum = 0;
    int highSum = 0;
    int lowSquared = 0;
    int highSquared = 0;
    
    for( int i = 0; i < numCities; i++ )
        {
	    lowSum += cityArray[i].lowTemp;
		highSum += cityArray[i].highTemp;
		lowSquared += lowSum * lowSum;
		highSquared += highSum * highSum; 
		averageLow = lowSum / numCities;
		averageHigh = highSum / numCities;
		stdDevLow = sqrt( lowSquared - ( ( lowSum *lowSum )/ numCities ) / ( numCities - 1 ) );
		stdDevHigh = sqrt( highSquared - ( ( highSum * highSum )/ numCities ) / ( numCities - 1 ) );
		}
    
    averageLow = lowSum / numCities;
	averageHigh = highSum / numCities;
    
    stdDevLow = sqrt( lowSquared - ( ( lowSum *lowSum ) / numCities ) / ( numCities - 1 ) );
	stdDevHigh = sqrt( highSquared - ( ( highSum * highSum ) / numCities) / ( numCities - 1 ) );
  
}





sorry it wouldn't attach in first post

here is the binary file.

Any help would be really appreciated!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Output will not come out correctly

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Output will not come out correctly

Posted 09 November 2012 - 07:45 PM

Hi, the record size in the file appears to be 36 bytes. Your struct will be 25 + 4 + 4 = 33. You can print the size of the struct to check what size is being read. It looks as if 3 bytes (at the end of the struct) has been added to align the struct.

The strings don't have null terminators either. You could test for letters with isalpha.

This post has been edited by #define: 09 November 2012 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Output will not come out correctly

Posted 09 November 2012 - 10:38 PM

I used a hex viewer to look at the file.
Going by the file, the structure is :-

struct cityInfo
{
    char cityName[28];
    int lowTemp;
    int highTemp;
};



Twenty-eight characters for the name.


Another problem though, is that what should be zeroes are spaces. So the null character in the name is a space, and zeroes in the number representation have the value of spaces.

For the name : you could read 36 bytes into a char buffer, then strncpy 28 characters to the city.name and set the last char to null. The names will still need tidying up a little.
For the numbers : in the last eight bytes of the buffer change any space characters with zero, and then cast from the appropriate positions in the buffer to the integers.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,351
  • Joined: 25-December 09

Re: Output will not come out correctly

Posted 10 November 2012 - 06:57 AM

Did you write this file, or is it a provided file? If you wrote this file please show the code that produced the file. If the file was provided you should have a exact description of the file, how each item was written (the number of bytes) so you can reverse the process.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1