A pointer pointer needs a pointer pointer

I am getting this error, Indirection requires a pointer-pointer ('int invalid') in this code, I think I am using empPtr incorrectly in this code, but I am not sure.

Thanks in advance guys.

I will also include my other classes from this link. https://gist.github.com/anonymous/08ff6c5284c179c9a323

My input text file looks like this.

123,John,Brown,125 Prarie Street,Staunton,IL,62088
124,Matt,Larson,126 Hudson Road,Edwardsville,IL,62025
125,Joe,Baratta,1542 Elizabeth Road,Highland,IL,62088
126,Kristin,Killebrew,123 Prewitt Drive,Alton,IL,62026
127,Tyrone,Meyer,street,999 Orchard Lane,Livingston,62088

      

Here is my main.cpp.

#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"

using namespace std;

bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);

int readFromFile(ifstream& in, Employee empArray[]);

void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);

int main() {

    ifstream fin;
    ofstream fout;

    if(!openFileForReading(fin, "employeesIn.txt")) {
        cerr << "Error opening employeesIn.txt for reading." << endl;
        exit(1);
    }

    if(!openFileForWriting(fout, "employeesOut.txt")) {
        cerr << "Error opeing employeesOut.txt for writing." << endl;
        exit(1);
    }

    Employee employeeArray[50];

    int employeeCount = readFromFile(fin, employeeArray);

    fin.close();

    writeToFile(fout, employeeArray, employeeCount);

    fout.close();

    cout << "Program successful." << endl << endl;


    return 0;
}

bool openFileForReading(ifstream& fin, const string& filename) {
    fin.open("employeesIn.txt");

    return (fin.is_open());
}

bool openFileForWriting(ofstream& fout, const string& filename) {
    fout.open("employeesOut.txt");

    return (fout.is_open());
}

int readFromFile(ifstream& in, Employee empArray[]) {
    int temp = 0;
    string eidText;
    string first;
    string last;
    string street;
    string city;
    string state;
    string zipcode;

    while(!in.eof()) {
        getline(in, eidText, ',');
        getline(in, first, ',');
        getline(in, last, ',');
        getline(in, street, ',');
        getline(in, city, ',');
        getline(in, state, ',');
        getline(in, zipcode);

        empArray[temp].setEid(stoi(eidText));
        empArray[temp].setName(first, last);
        empArray[temp].setAddress(street, city, state, zipcode);

        temp++;
    }

    return temp;
}

void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {

    for (int i = 0; i < numberOfEmployees; i++){
        out << "Employee Record: " << empArray[i].getEid()
        << endl
        << "Name: " << empArray[i].getName()
        << endl
        << "Home Address: " << empArray[i].getAddress() 
        << endl
        << endl;
    }
}

      

+3


source to share


2 answers


You have

int empPtr = 0;

      

followed by



(*empPtr).setEid(stoi(eidText));

      

which is clearly the cause of this error.

+2


source


This line is incorrect.

Employee empPtr = employeeArray[50];

      

The maximum allowed index for employeeArray

is 49

.

To get the first element of an array use:



Employee empPtr = employeeArray[0];

      

To get the last element of an array use:

Employee empPtr = employeeArray[49];

      

More details on C and C ++ array access can be found at http://www.augustcouncil.com/~tgibson/tutorial/arr.html and http://www.tutorialspoint.com/cprogramming/c_arrays.htm .

+1


source







All Articles