I'm trying to read numbers from a data file into array but when I compile and run nothing shows up.
what did I do wrong. I have to do all the reading from the function readData and I need to get rid of the header line but I just put that there for myself. Heres the data file

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

#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]);
void avgCalc(int homeInfo[][PRICE]);
void sortArray();
void writeResult();

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

    //Declare variables
    string line;
    int homeInfo[10][2];
    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);
    //avgCalc(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++)
            homeInfo[i][j];
        i++;
    }
}

void avgCalc(int homeInfo[][PRICE])
{
    float avg = 0;
    float sum = 0;

    for(int i = 0; i < HOME_NUMBERS; i++)
        sum+=homeInfo[i][1];
    avg = sum/PRICE;

    cout << "The average of the prices is: " << avg << endl;
}

Edited 3 Years Ago by thePAkid: missed it

Nothing shows up because there is nothing in the program above that either displays on the console or writes to file. Uncomment the call to the average function.

Your reading function is missing the actual reading. In the loop, you access the array elements, but don't do an input to them.

In your averaging function, you need to divide the sum by HOME_NUMBERS (10), not PRICE (2).

In the read function would I declare variables or do I use HOME_NUMBERS and PRICE. Becuase I did declare new variables but the compiler says that they aren't declared and it also says that there is no operator to match inFile >> etc...

I took your code posted above and with the few corrections I noted before, got it to work fine. HOME_NUMBERS and PRICE are visible to the reading function, and there darn sure is an operator for inFile >> homeInfo[i][j]; Be sure you don't make any typing errors.

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