Trouble reading from text file into array using pointers

I'm having a problem figuring out how to read the information from a text file and populate an array of structs using a pointer. My file and struct has 3 parts: account number, account name, and the amount in that account. I'm getting an error I don't know how to fix. This really should be easy, but I'm just missing it somewhere. I greatly appreciate any help, tips, or comments!


error:  Line 59:  invalid types 'budget [15][budget*]' for array subscrpt 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
struct budget
{
    int budget_num;
    string name;
    float budget_value;
};

int main()
{
    budget ary[15];
    budget *ptr;

    ptr = &ary[0];

    //Open inFile
    inFile.open("ledger.dat");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    while(!inFile.eof())
    {
        for(ptr = &ary[0]; ptr < &ary[15]; ptr++)
        {
            *ptr = getline(inFile, ary[ptr]); //Line 59
        }
    }

    //Close the files
    inFile.close();

    //Exit program
    return EXIT_SUCCESS;
}


I have also tried the following code and get a different error.

1
2
3
4
5
6
7
    while(!inFile.eof())
    {
        for(ptr = &company[0]; ptr < &company[15]; ptr++)
        {
        getline(cin, ptr.budget_num, ptr.name, ptr.budget_value);
        }
    }



error:  request for member 'budget_num' in 'ptr', which is of non-class type 'budget*'
If the last error you are using . notation on a pointer. To access the object pointed to you need ptr->. Or (*ptr).

*ptr = getline(inFile, ary[ptr]); //Line 59

Is incorrect because you used a pointer in []. This should be a number for the index you wish to access.

And when you fix that problem you will have another. You're using a function that gets a std::string from a stream. But you put in one of your budgets in the function where the string should be.
getline(stream,string)
Last edited on
Ok, I'm really confused. :(
in order to access what the ptr points to, you need to dereference it, (*ptr) would be what the ptr points to.

if you do ptr->name, it is the same as doing (*ptr).name, which is the same as doing budget.name, but doing ptr.name is illegal, ptr is simply a memory address, it has no member variables.
I changed the code, but now I'm getting a different error.


error: no matching function for call to 'getline(std::ifstream&, int&, std::string&, float&)


1
2
3
4
5
6
7
    while(!inFile.eof())
    {
        for(ptr = &company[0]; ptr < &company[15]; ptr++)
        {
        getline(inFile, (*ptr).budget_num, (*ptr).name, (*ptr).budget_value);
        }
    }
check the documentation on this website for the function "getline", see what arguments it accepts and how many.
Your use of getline is not what that function is for, and the arguments don't match, you can't do what your trying to do with getline. You need to get a std::string using getline, then parse the string yourself.
Ok, I changed my code and it doesn't give any errors, but I can't get it to print it out what is in there.

1
2
3
4
5
6
7
8
9
10
    while(!inFile.eof())
    {
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
        //getline(inFile, (*ptr).budget_num, (*ptr).name, (*ptr).budget_value);
        cin >> (*ptr).budget_num;
        cin >> (*ptr).name;
        cin >> (*ptr).budget_value;
        }
    }
after every call to cin >> use a cin.ignore().

Also you have while (!inFile.eof() ) this loop either never runs or never ends depending on the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <iostream>
#define SIZE 3
using namespace std;

struct budget
{
int budget_num;
string name;
float budget_value;
};

int main()
    {

    budget company[SIZE];
    for ( int i = 0; i < SIZE; ++i )
        {
        cin >> company[i].budget_num;
        cin.ignore();
        getline(cin, company[i].name);
        cin >> company[i].budget_value;
        cin.ignore();
        }

    for ( int i = 0; i < SIZE; ++i )
        {
        cout << company[i].budget_num << ' ' << company[i].name << ' ' << company[i].budget_value << '\n';
        }
    std::cin.get();
    }


You should think of adding error checking to the cin >> lines. If you enter something invalid it will do undefined behavior.
Last edited on
I tried two different ways of doing this and I get the same junk out for both:


Number: 4669840
Name:   
Value:  3.76441e-039

Number: 4218308
Name:   
Value:  6.52879e-039

Number: 2686408
Name:   
Value:  6.52879e-039

Number: 0
Name:   
Value:  6.52879e-039

Number: 0
Name:   
Value:  8.40779e-045

Number: 4692468
Name:   
Value:  5.88904e-039

Number: 4639716
Name:   
Value:  0

Number: 0
Name:   
Value:  5.98096e-039

Number: 4659100
Name:   
Value:  8.40779e-045

Number: 4659100
Name:   
Value:  6.5271e-039

Number: 4692468
Name:   
Value:  3.76451e-039

Number: 4657896
Name:   
Value:  6.57555e-039

Number: 6
Name:   
Value:  8.40779e-045

Number: 16
Name:   
Value:  5.88904e-039

Number: 4642264
Name:   
Value:  0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    while(!inFile)
    {
//Same output for this:
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
            cin >> (*ptr).budget_num;
            cin >> (*ptr).name;
            cin >> (*ptr).budget_value;
        }
//As output for this:
//        for ( int i = 0; i < SIZE; ++i )
//        {
//            cin >> (company[i].budget_num);
//            cin.ignore();
//            getline(cin, company[i].name);
//            cin >> company[i].budget_value;
//            cin.ignore();
//        }

    }

    for(int i = 0; i < SIZE; i++)
    {
    cout << "Number: " << company[i].budget_num << endl;
    cout << "Name:   " << company[i].name << endl;
    cout << "Value:  " << company[i].budget_value << endl;
    cout << endl;

    outFile << "Number: " << company[i].budget_num << endl;
    outFile << "Name:   " << company[i].name << endl;
    outFile << "Value:  " << company[i].budget_value << endl;
    outFile << endl;
    }
Topic archived. No new replies allowed.