OutFile to file help
Dec 15, 2012 at 12:11am UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#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
}
*/
Dec 15, 2012 at 12:39am UTC
I think you just have to format it.
You know how when you don't send it to a file but just use the black box to see what is going on?
Just do the same thing.
I didn't look at your code so much but it sounds like this is what you are asking. If not sorry.
Topic archived. No new replies allowed.