I'm trying to outFile to houseData.txt. I did that fine but now all the data is all on the same line 10 times. What did do wrong in my code?

House Price
Number
------ -----
329155000235000
459160000235000
505185000235000
500215000235000
345210000235000
456305000235000
344405000235000
501355000235000
401190000235000
300170000235000

I need it to look like this

House Price
Number
------ -----
329 155000
459 160000
505 185000
500 215000
345 210000
456 305000
344 405000
501 355000
401 190000
300 170000

The average price is: 235000

Here's my code. Don't mind the the sortArray function I'm still working on that

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

const int HOME_NUMBERS = 10;
const int PRICE = 2;

//void programDescription();
void readData(ifstream &inFile,int homeInfo[][PRICE]);
float avgCalc(int homeInfo[][PRICE]);
//void sortArray();
void writeResult(ofstream &outFile, int homeInfo[][PRICE]);

int main()
{
    //Program Description
    //programDescription();

    //Declare variables
    int homeInfo[HOME_NUMBERS][PRICE];
    ofstream outFile;
    ifstream inFile;

    //Open file for input
    inFile.open("prices.txt");
    if(inFile.fail())
    {
        cout << "Error opening file" << endl;
        exit(1);
    }


    //Opens file for output
    outFile.open("homeData.txt");
    if(outFile.fail())
    {
        cout << "Error opening file" << endl;
        exit(1);
    }

    //Process data as read and display in tabular format


    //Function call
    readData(inFile,homeInfo);
    cout << "The average price is: " << avgCalc(homeInfo) << "\n";
    writeResult(outFile,homeInfo);


    return 0;
}


void readData(ifstream &inFile, int homeInfo[][PRICE])
{
    int i = 0;
    while(i < HOME_NUMBERS&&!inFile.eof())
    {
        for(int j = 0; j < PRICE; j++)   
            inFile >> homeInfo[i][j];               
        i++;

    }

}

void writeResult(ofstream& outFile, int homeInfo[][PRICE])
{
    //Display header line
    outFile << endl
        << "House     Price" << endl
        << "Number              " << endl
        << "------        -----" << endl;
    for(int i = 0; i < HOME_NUMBERS; i++)
    {
        for(int j = 0; j < PRICE; j++)
            outFile << homeInfo[i][j];
            outFile << avgCalc(homeInfo);

        outFile << endl;
    }
}

float avgCalc(int homeInfo[][PRICE])
{
    float avg;
    int total = 0;
    for(int i = 0; i < HOME_NUMBERS; i++)
        total+=homeInfo[i][1];
    avg = float (total)/HOME_NUMBERS;
    return avg;
}

/*void sortArray(int homeInfo[][PRICE)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( ); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              { 
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}
*/

The problem is here:

for(int i = 0; i < HOME_NUMBERS; i++)  {
    for(int j = 0; j < PRICE; j++)//this loop writes the prices
        outFile << homeInfo[i][j];
        outFile << avgCalc(homeInfo);//This line writes the avgCalc after writing the prices
    outFile << endl;
}

First, you are writing the avgCalc after writing both prices. You only need to supress that line. The other problem is that the ofstream doesn't write automatically the space character. You must add outFile << ' ' somewhere.

because thats your average price that you are receiving when you call avgCalc(homeInfo);. It is beeing printed because you call outFile << avgCalc(homeInfo); before ending each line.

This article has been dead for over six months. Start a new discussion instead.