Hello I'm trying read data from a file and display it into 3 arrays coloums. however I keep on getting an error on line 33 stating [expected primary-expression before "int,double,int,int"] [ISO C++ forbids declaration of `readHousehold' with no type] I cant figure it out but I know it has to do with my functions. Thank you so much!

here is the prompt

The results of a survey( in a file) of the households in your township are available for public scrutiny. Each record contains data for one household, including a four-digit integer identification number, the annual income for the household , and the number of household members ( int, double, int). Write a program to read the survey results into three arrays and perform the following analysis (irrelevant as of now).

Here's my code

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

int readHousehold(ifstream &inFile,int idhouseholdArray[], double incomeArray[], int membersArray[], int size);
void   displayhouseData(int idhouseholdArray[], double incomeArray[], int membersArray[]);

const int OK = 0;

int main()
{
  int rc;

  int idhouseholdArray[24] = {1041,1062,1327,1483,1900,2112,2345,3210,3600,3601,4724,6217,9280,1000,1200,5601,5724,5217,5280,5000,5200,5230,6641,7000};

  double incomeArray[24] ={12180,13240,19800,22458,17000,18125,15623,3200,6500,11970,8900,10000,6200,30000,35000,51970,66900,10000,70000,100000,25000,120000,85000,45500};

  int membersArray[24] ={4,3,2,8,2,7,2,3,5,2,3,2,1,3,2,9,3,2,1,6,3,6,7,4};

    ifstream inFile;                             // create an input file object

    cout << fixed
         << showpoint
       << setprecision(2);

    inFile.open("Bonus-Program.txt");                // open the file read

    if (!inFile.fail())                         // if open was successful
    {                                           // execute statements in true path
        rc = readHousehold(inFile, int idhouseholdArray[24], double incomeArray[24], int membersArray[24], int size);
        if (rc == OK) 
            displayhouseData(idhouseholdArray, incomeArray, membersArray);
        else
            cout << "Error: Incorrect house data in file!!\a" << endl;         
    }
    else                                        // if fail was not successful
    {                                           // execute statements in false path
        cout << "File \"bonus.txt\" "
             << "not found."
             << endl;
    }

    system("PAUSE");

    return 0;
}

readHousehold(ifstream &inFile,int idhouseholdArray[], double incomeArray[], int membersArray[],int size)
{
     int count = 0, rtnCode = 0;
     double temp;

     inFile >> temp;
     while ((!inFile.fail()) && (count < size))
     {
        idhouseholdArray[count] = temp;
        count++;
        incomeArray[count] = temp;
        count++;
        membersArray[count] = temp;
        count++;


        inFile >> temp;
        if (count == size)
        {
           if (!inFile.fail()) 
               rtnCode = 1;
        }
     }

     if (count < size)
         rtnCode = 1;

     return rtnCode;
}


void   displayhouseData(int idhouseholdArray[], double incomeArray[], int membersArray[]);
{


}

Dani AI

Generated

Good move fixing the call like suggested — that removed the compile error. The blank output now usually means one of two things: the display routine never prints anything, or the reader never fills the arrays. In the posted code the display function is empty and the reader increments the index incorrectly. Fix both and the program will show the data.

A safe pattern for the reader: read one full record per loop into appropriately typed temporaries, store them at the same index, then increment the index once. Also make sure the function definition has the return type (your prototype is int, so the definition must start with int). Example approach:

int readHousehold(std::ifstream &fin, int idA[], double incA[], int memA[], int size) {
    int count = 0;
    int id;
    double inc;
    int mem;
    while (count < size && (fin >> id >> inc >> mem)) {
        idA[count] = id;
        incA[count] = inc;
        memA[count] = mem;
        ++count;
    }
    return (count == size ? 0 : 1); // match your OK/error convention
}

Make display actually print rows and know how many entries to print (either pass size or return the number read). Example:

void displayhouseData(int idA[], double incA[], int memA[], int size) {
    for (int i = 0; i < size; ++i)
        cout << idA[i] << '\t' << fixed << setprecision(2) << incA[i] << '\t' << memA[i] << '\n';
}

Practical checks: define and pass a real size (e.g., const int SIZE = 24), verify the input file name/path and that each record is id income members in that order, and temporarily print a debug line inside the read loop to confirm records are being read. That will make it trivial to see whether the problem is in reading or in displaying.

Recommended Answers

All 2 Replies

Get rid of the types in the function call (i.e. int, double). Also get rid of the array brackets. Those go in the function definition, not the function call. Line 33 should look more like this. Note the lack of types and the lack of brackets.

rc = readHousehold(inFile, idhouseholdArray, incomeArray, membersArray, size);

It is now compliling but it doesnt display the array, did I fill it properly? It gives me a blank

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.