I'm trying to read information from a text file and populate an array of structs using a pointer. I keep getting an error and I'm not sure how to fix it. Any help, comments, or tips are greatly appreciated! Thanks in advance for your time and help!

error: request for member 'budget_num'in 'ptr', which is of non-class type 'budget*'

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

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
{
    //Declare variables
    budget company[15];
    budget *ptr;
    //string MyString = "";

    ptr = &company[0];

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

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

    while(!inFile.eof())
    {
        for(ptr = &company[0]; ptr < &company[15]; ptr++)
        {
        cin(inFile, ptr.budget_num);
        cin(inFile, ptr.name);
        cin(inFile, ptr.budget_value);
        //getline(inFile, MyString); //Used to test to see if the information was coming in
        }
    }

       //Close the files
    inFile.close();

    //Exit program
    return EXIT_SUCCESS;
}

I think your ptr needs to be *ptr for it to work. I am a noob at programming, but that is what I would try next.

I tried that and I received the same error. Thanks for the suggestion.

I also tried the following code and it was unsuccessful:

    while(!inFile.eof())
    {
        for(ptr = &company[0]; ptr < &company[15]; ptr++)
        {
        cin >> *ptr.budget_num;
        cin >> *ptr.name;
        cin >> *ptr.budget_value;
        }
    }

there is no such index value as company[15], the indices are numbered 0, 1, 2, ... 14. A safer loop is to use integer, not pointer, in the for statement. Attempting to use ptr like you did is not safe because it depends on how the computer's memory is laid out

for(int i = 0, ptr = company; i < 15; i++, ptr++)
{

|

Finally, declare a const int to represent the number of elements in company so that you can easily change its value whenever you wish with little effort.

I have changed my code and I get the exact same junk out of each of them. At least it is reading something in, just not sure what it is... :(

  while(!inFile)
    {
        //Same junk output for this
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
            cin >> (*ptr).budget_num;
            cin >> (*ptr).name;
            cin >> (*ptr).budget_value;
        }

        //As 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;
    }

post the whole program. This works ok for me using vc++ 2012 RC

#include<string>
#include <iostream>
#include <fstream>
using namespace std;

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

const int SIZE = 15;
int main()
{
     budget company[SIZE], *ptr;
     ifstream inFile("ledger.dat");
     ofstream outFile;
  //while(!inFile)
    {
        //Same junk output for this
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
            cin >> (*ptr).budget_num;
            cin >> (*ptr).name;
            cin >> (*ptr).budget_value;
        }

        //As 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;
    */
    }
}

ignore the above -- you started another thread.

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.