-2

I have some trouble displaying the file I've read into an array of structure.

This is my struct:

struct Employee {
    char staffId[5];
    char fullName[30];
    char phoneNum[15];
    char address[40];
    char email[30];
 };

This is my text file (columns separated by "tab"):

1   Clark Kent      012-1449326 221, Jalan Pudu, Kuala Lumpur   clark_kent@gmail.com
2   Bruce Wayne     013-9817470 65, Jalan Jejaka, Kuala Lumpur  bruce_wayne@hotmail.com
3   Peter Parker    017-6912495 26, Jalan Rajabot, Kuala Lumpur peterparker@zoho.net
4   Yeoman Prince   014-1374040 22, Jalan 1/109e, Kuala Lumpur  yeoman_prince@yahoo.com
5   Tony Stark      016-7473151 21, Jalan Pandan, Kuala Lumpur  tonystark@zoho.net
6   Selina Kyle     012-4040928 Wisma Cosway, Kuala Lumpur selina_kyle@gmail.com
7   Steve Rogers    018-9285217 Desa Pandan, Kuala Lumpur   steverogers@hotmail.com
8   Alan Scott      019-5569400 2, Jalan U1/17, Shah Alam   alanscott@gmail.com
9   Britt Reid      011-7876738 43, Jalan SS2/23, Petaling Jaya brittreid@yahoo.com
10  Darcy Walker    011-4042788 Blok B, Setapak, Kuala Lumpur   darcywalker@gmail.com
11  Reed Richards   019-2299339 Menara U, Bangsar, Kuala Lumpur reedrichards@zoho.net
12  Barbara Gordon  017-2297980 The Boulevard, Kuala Lumpur barbaragordon@gmail.com
13  Don Diego Vega  012-4142987 10, Jalan Wangsa, Kuala Lumpur  donvega@zoho.net
14  Billy Batson    013-9200151 122, Jalan Jejaka, Kuala Lumpur billybatson@hotmail.com
15  Barry Allen     017-7928822 Wisma Laxton, Kuala Lumpur  barryallen@gmail.com
16  Stanley Beamish 014-9177437 203, Sunwaymas, Batu Caves  stanleybeamish@yahoo.com
17  Dick Grayson    017-4023800 Pekeliling Bus, Kuala Lumpur    dickgrayson@hotmail.com
18  James Howlett   012-7816910 Sri Hartamas, Kuala Lumpur  jameshowlett@zoho.net
19  Hal Jordan      013-3439897 302, Jalan Ampang, Kuala Lumpur haljordan@yahoo.com
20  Scott Summers   012-9057100 Menara Summit, Subang Jaya  scottsummers@zoho.net

My code to read from the text file:

ifstream in("list.txtwith extension");
Employee *totaldata = new Employee[value + 1];
string line;
while (getline(in, line)) {
    istringstream iss(line);
    string token;

    while (getline(iss, token, '\t')) {
        // if you just want to print the information
        cout << token << '\t';
        // or you can store it in an Employee object
        in.getline(totaldata[value].staffId, 5, '\t');
        in.getline(totaldata[value].fullName, 30, '\t');
        in.getline(totaldata[value].phoneNum, 15, '\t');
        in.getline(totaldata[value].address, 40,'\t');
        in.getline(totaldata[value].email, 30, '\t');
            value++;
    }
    cout << endl;

    for (int i = 0; i < value; i++) 
    {
        cout << totaldata[value].staffId << "\t"
             << totaldata[value].fullName << "\t"
             << totaldata[value].phoneNum << "\t"
             << totaldata[value].address << "\t"
             << totaldata[value].email << endl;
    }

I can't seem to input it in an array of structure and can't display it out?

  • 1
    `in.getline` seems wrong to me. please make a [mcve]. – apple apple Aug 11 '18 at 08:06
  • Why don't you use `std::string` – JVApen Aug 11 '18 at 09:13
  • Did you just put a bunch of names and their email adresses online? If it just serves as example, use `...@example.com`, which is a domain explicitly reserved for stuff like that. – Ulrich Eckhardt Aug 11 '18 at 09:15
  • @UlrichEckhardt unless spiderman, superman and all the other super heroes moved to kuala lumpur I would assume that this is fake data ;) – 463035818_is_not_an_ai Aug 11 '18 at 10:22
  • Did my answer not help? If so, why not asking questions/comments. If it helps, why not accepted and or upvoted? – Klaus Aug 11 '18 at 18:53
  • @Klaus: I'd rather delete this low-quality question, which doesn't even have a [mcve] as actually required. Perhaps it could be moved to codereview.stackexchange.com though, but here, it is misplaced. That said, yes, your suggestions are all valuable! – Ulrich Eckhardt Aug 14 '18 at 16:01
  • @UlrichEckhardt Since the code does not work, Code Review won't take it either. – Mast Aug 14 '18 at 16:03

1 Answers1

1

You have a long list of bugs in your prog!

1) the data you provide has no tabs ( maybe only a problem by copy & paste to SO ) so please be shure for your next post to SO your data is formatted as described!

2) your field "phone number" is to short. There are longer phone numbers present in the data records

3) you should use constants instead of hard coded values for the size of fields. If you change the field size on one place, you are in danger to miss that on other places! see example!

4) the last element of every line would not contain a tab at the end I believe! So the last getline in the line should read until end not until tab!

5) Your loop for printing the data has to use the loop var i and not value which is the number of the first element BEHIND! your data!

6) Your reading loop was totally confused. I reduced it a bit ;)

7) you use a fixed size of elements/records. But you do not check for overrun! So if value becomes 11 your program crashes!

8) As a general remark: Your program is C code! You don't use objects but plain data structures and functions which directly write into that data structures. That is not object oriented and has a lot of problems like overwriting field size, not checking for ranges and having inconsistent data!

I added a more c++ style example at the end. This example is also not really perfect as it takes a lot of copies of strings during processing which makes it slower than the good old c-style code. But it should help to give you an idea to use objects which knows how to do actions itself like reading/writing the own data. As said: Only as a start point and far away from really well!

struct Employee {
    static const unsigned int staffIdLen = 5;
    static const unsigned int fullNameLen = 30;
    static const unsigned int phoneNumLen = 20;
    static const unsigned int addressLen = 40;
    static const unsigned int emailLen = 30;

    char staffId[staffIdLen];
    char fullName[fullNameLen];
    char phoneNum[phoneNumLen];
    char address[addressLen];
    char email[emailLen];
};


int main()
{
    const unsigned int maxRecords = 10;
    std::ifstream in("f.txt");
    Employee *totaldata = new Employee[ maxRecords ];
    std::string line;
    unsigned int value = 0;

    while ( std::getline(in, line) )
    {
        std::cout << "Read from file:" << line << std::endl;

        std::istringstream iss(line);

        iss.getline(totaldata[value].staffId, Employee::staffIdLen, '\t');
        iss.getline(totaldata[value].fullName, Employee::fullNameLen, '\t');
        iss.getline(totaldata[value].phoneNum, Employee::phoneNumLen, '\t');
        iss.getline(totaldata[value].address, Employee::addressLen,'\t');
        iss.getline(totaldata[value].email, Employee::emailLen);
        value++;
        if ( value == maxRecords ) break;
    }

    std::cout << "Finish reading file " << std::endl;

    for (int i = 0; i < value; i++)
    {
        std::cout << "----------------start-----------" << std::endl;
        std::cout << totaldata[i].staffId << "\t"
            << totaldata[i].fullName << "\t"
            << totaldata[i].phoneNum << "\t"
            << totaldata[i].address << "\t"
            << totaldata[i].email << std::endl;

        std::cout << "--------------end--------------" << std::endl;

    }
}

c++ example:

class Employee {
    private:
        std::string staffId;
        std::string fullName;
        std::string phoneNum;
        std::string address;
        std::string email;

    public:
        friend std::istream& operator>>( std::istream&, Employee& );
        friend std::ostream& operator<<( std::ostream&, const Employee& );
};

std::istream& operator>>( std::istream& is, Employee& e)
{
    std::getline( is, e.staffId, '\t');
    std::getline( is, e.fullName, '\t');
    std::getline( is, e.phoneNum, '\t');
    std::getline( is, e.address, '\t');
    std::getline( is, e.email );

    return is;
}

std::ostream& operator<<( std::ostream& os, const Employee& e)
{
    os << e.staffId << e.fullName << e.phoneNum << e.address << e.email << std::endl;

    return os;
}

int main()
{
    std::ifstream in("f.txt");
    std::vector<Employee> employees;

    do
    {
        Employee e;
        in >> e;

        if ( in.fail() ) break;
        employees.push_back( e );
    } while( 1 );

    for ( auto& e: employees)
    {
        std::cout << e << std::endl;
    }

}
Klaus
  • 24,205
  • 7
  • 58
  • 113