0

So I'm creating a dynamic array list class pointer. But my constructors doesn't read anything from the file. It's not showing any error and I was able to run it. So i'm not sure if i'm missing something to link the pointer.

Implementation code

#include "S.h"

void S::setSize(int size1)
{
    size=size1;
}

S::S (ifstream& fin)
{
    int s;
    fin>>s;
    size=s;

    S *ptr = new S[s];

    string fname;
    string lname;
    string address;
    for(int i = 0; i<s; i++)
    {
        fin>>fname>>lname;
        getline(fin,address);

        ptr[i].setName(fname); //changed to i
        ptr[i].setLname(lname);
        ptr[i].setAddress(address);

        //s++;
    }
    //cout<<list[0].firstname;


list = ptr;

}
void S::print(ofstream& fout)
{
    for(int i=0; i<size; i++)
    {
        fout<<list[i].getName()<<"   "<<list[i].getLname()<<"   "<<list[i].getAddress()<<endl;
    }
}

driver code

#include "S.h"

int main()
{
    ifstream fin;
    ofstream fout;

    
}

so this is my class code. On my function implementation i have the setters implemented as well. like: void S::setName (string name1){list->firstname=name1;}

class S\
{
private:
    int size;
    string firstname;
    string lastname;
    string address;

    S* list;

public:

    S ();
    S (ifstream& fin);

void setName(string);
    void setSize(int);
    void setLname(string);
    void setAddress(string);

    string getName(){return firstname;}
    string getLname(){return lastname;}
    string getAddress(){return address;}


    int getSize(){return size;}


    void print(ofstream&);


};
ceerie
  • 1
  • 2
  • Where is the class definition? Have a look at how to make a [mcve]. – super Oct 23 '20 at 06:54
  • You don't check if the file is opened, so it's possible it isn't. You should also read this: https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – Retired Ninja Oct 23 '20 at 06:55
  • Almost certainly the file open has failed, you should always check for that. And the most common reason for a file open failing is that your program is looking for the file in a different location to where you put the file. – john Oct 23 '20 at 06:59
  • Okay. I did try to add if file is open..but it still not working with the file open. – ceerie Oct 23 '20 at 07:03
  • @ceerie So the file is opening successfully? Then it's time to check of the reads are successful. Or maybe the print routine is wrong somehow (but I can't see how). Or maybe the class is declared wrongly somehow (you haven't shown the class declaration). This is just normal debugging, many things could be wrong, you just have to narrow down the possibilities until you find the problem. Using a debugger might help. – john Oct 23 '20 at 07:10
  • @ceerie Or you can post a complete program, and your input file. And then someone else can run the code and find the problem. – john Oct 23 '20 at 07:12
  • Im using eclipse, so the file goes exactly with the same folder with this – ceerie Oct 23 '20 at 07:22
  • @john I added the class part, please check if you can see anything wrong with it. – ceerie Oct 23 '20 at 07:27
  • @ceerie So there's a clear problem with your class, but it's a different problem. The problem with your class is that it's trying to be two things at once. It's both an individual customer (it has a name and an address) but it's also a list of customers (it has a size and a pointer to an array). Really it should be two separate classes, you'll get marks deducted for that. – john Oct 23 '20 at 08:25
  • @ceerie I would still say that the most likely explanation for the problem that is happening now is that you aren't opening the file correctly, or that you aren't reading the file correctly. To help with either of those I need to see the code you wrote that checks if the file is open, and I'd need to see the input file (at least a small part of it). – john Oct 23 '20 at 08:27
  • @ceerie I see from the other post that you are having trouble opening the file and trouble printing error messages. So many things can go wrong all at once! It's absolutely not the case that the file must be in the `same folder` (same as what exactly?). I'm afraid every system is different, so you need to find out where to put the file on your system. Eclipse will have a setting that you can change to tell it where to look for files, not sure what that is however. – john Oct 23 '20 at 08:33

1 Answers1

3

You should change your for cycle. You always use 's' as index.

for(int i = 0; i<s; i++)
{
    fin>>fname>>lname;
    getline(fin,address);

    ptr[i].setName(fname);
    ptr[i].setLname(lname);
    ptr[i].setAddress(address);

    // ptr[s].setName(fname);
    // ptr[s].setLname(lname);
    // ptr[s].setAddress(address);
}
Anton Shwarts
  • 468
  • 1
  • 7
  • @ceerie could you post minimal reproducible example? – Anton Shwarts Oct 23 '20 at 07:10
  • @ceerie i tried run it (with several additions to compile) and it's work fine. i suppose a problem in the file. add this piece of code before read file ```if (fin.is_open()) {cout << "file is opened" << endl;}``` in ```SpartansCommunication (ifstream& fin)``` – Anton Shwarts Oct 23 '20 at 07:38
  • @ceerie if you have exactly this code ```if(!fin) cout< – Anton Shwarts Oct 23 '20 at 08:23