0

I'm a beginner and I'm now learning all about arrays and experimenting different ways to implement it. This time, I really wanted to know how to return a string array in c++ without using vector for research purposes. I tried implementing pointers as a way to return a string array but it is giving me a runtime error stating that the string subscript is out of range. Kindly advice if I made the wrong way of returning the string array and provide better solutions for this.

Here is the code the Employee.h:

    #pragma once
    #include<string>
    #include<iostream>

    class Employee
    {
    private:
    static const int recordSize = 100;
    static const int fieldSize = 4;
    std::string record[recordSize][fieldSize];

    public:
    Employee();
    ~Employee();
    std::string * employeeReadData();

    };

Here is the Employee.cpp

 Employee::Employee()
 {
 }

std::string * Employee::employeeReadData() {
std::ifstream inFile;
inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt");

static std::string recordCopy[recordSize][fieldSize];

for (int index = 0; index < recordSize; index++) {
    for (int index2 = 0; index2 < fieldSize; index2++) {
        inFile >> record[index][index2];
    }
}

for (int index = 0; index < recordSize; index++) {
    for (int index2 = 0; index2 < fieldSize; index2++) {
        recordCopy[index][index2] = record[index][index2];
    }
}

inFile.close();

std::string * point = * recordCopy;

return point;
    }

Here is main():

    int main()
    {
    Employee emp;


    std::string* p = emp.employeeReadData();

    cout << p[0][0] <<endl;
    cout << p[0][1] << endl;
    cout << p[0][2] << endl;
    cout << p[0][3] << endl;

    return 0;
   }

employee-info.txt:

    ID           Firstname            Lastname                 Sales
     1             Reynard             Drexler             150000.00
     2              Joseph               Bones             250000.00
11
  • Do you know how to use the debugger? What line is is encountering it on exactly? Commented May 3, 2019 at 3:25
  • 4
    I really wanted to know how to return a string array in c++ without using vector for research purposes. -- You can't return arrays, unless you return a std::array or some class that wraps the array concept. I guess that ends the research. Commented May 3, 2019 at 3:28
  • Also, I should mention that you are doing some weird things in your code, like returning point in Employee::Employee(). Also, declaring static std::string recordCopy might let you get away with returning the address (since it's static) but you might want to find a better way to do it. Commented May 3, 2019 at 3:28
  • @PaulMcKenzie You can return a pointer, though--which can be treated like an array from the caller's point of view sometimes. Commented May 3, 2019 at 3:30
  • A pointer is not an array. Trying to not have the OP believe they are doing something that isn't really being done. Commented May 3, 2019 at 3:30

1 Answer 1

1

provide better solutions for this.

Well, you can use the techniques shown in this answer, and wrap the array into a struct within your Employee class.

#include <string>

class Employee
{
   public:
       struct EmployeeRecord
       {
           static const int recordSize = 100;
           static const int fieldSize = 4;
           std::string record[recordSize][fieldSize];
       };
    private:           
       EmployeeRecord emp_record;

    public:           
       Employee() {}
       ~Employee() {}
       EmployeeRecord& employeeReadData();
};

Then in the implementation:

 #include <fstream>

 Employee::Employee() {}

 Employee::EmployeeRecord& Employee::employeeReadData() 
 {
    std::ifstream inFile;
    inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt");
    for (int index = 0; index < EmployeeRecord::recordSize; index++) 
    {
        for (int index2 = 0; index2 < EmployeeRecord::fieldSize; index2++) 
            inFile >> emp_record.record[index][index2];
    }
    return emp_record;
}

Then:

int main()
{
    Employee emp;
    auto& p = emp.employeeReadData(); // return reference to the struct that has the array

    std::cout << p.record[0][0] << std::endl;
    std::cout << p.record[0][1] << std::endl;
    std::cout << p.record[0][2] << std::endl;
    std::cout << p.record[0][3] << std::endl;
    return 0;   
}

There is no usage of pointers, and also no usage of vectors. This is about as simple as you can get without usage of pointers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.