hey guys its 1.30 am and I just can't see where the problem here !
the text file contains just brand , model and price, please help !

#include<iostream>
#include<fstream>

using namespace std;

    struct computer{
    char * brand ;
    char * model;
    double  price;
    };

    void readin(computer * , int );
    void printf(computer * );
    int main ()
  {
    computer * pc ;
    computer * temp;
    int size;
    char filename [30];
    
    cout<< " please enter the file name : "<<endl;
    cin>>filename;
    readin(pc, size);
    printf(pc);
   
    delete [] pc;
    delete [] temp;
    
    system("pause");
    return 0;
}

// reading function 
    void readin(computer * , int )
    {
    computer * pc ;
    computer * temp;
    int size;
    ifstream fin;
    char filename [30];
    fin.open(filename);
    fin>>size;
    
    pc = new computer[size];
    temp = pc;
    for (int i =0; i<size; i++)
    {
    (*temp).brand = new char [15];
    (*temp).model = new char [15];
    
    fin>>(*temp).brand>>(*temp).model>>(*temp).price;
    temp++;
    
    }
    fin.close();
    
    temp = pc;
    for ( int i = 0; i<size; i++)
    {
    delete [] (*temp).brand;
    delete [] (*temp).model;
    temp++;
    
    }
    delete [] pc;
    delete [] temp;
    }
 // printing function 
    void printf(computer * )
    {

    computer * pc ;
    computer * temp;
    int size;

    for(int i= 0; i<size; i++)
    {
    cout<<"brand:"<<(*temp).brand<<"model:"<<(*temp).model<<"price:"<<(*temp).price <<endl;
    temp++;
    }
    temp = pc;
    for ( int i = 0; i<size; i++)
    {
    delete [] (*temp).brand;
    delete [] (*temp).model;
    temp++;
    
    }
    temp = pc;
    
    delete [] pc;
    delete [] temp;


    }

At what point do you get the seg fault?

One hint is that your filename is not making it into your readin function at all. You've appeased the compiler by redeclaring it but there's nothing in it. I suspect there are other areas as well. If you're going to use the ol-fashioned char * strings it's best to use getline so you don't overrun your array.

Continuing, you don't have your parameters in your definition of readin() laid out properly on line 34. They need to have names and the names must match what you are using in the function (so your int parameter could be called size) and not redeclared there.

In fact I didn't understand what you mean using this program.
I see that there is a statement

fin.open(filename);

which tries to open a file with an uninitialized name.
Then,

fin >> size;

This will try to copy the value of fin, which is an ifstream variable to size. What is the use of that?
Can u pls post a little more details?

Shibu
<<snip>>

hey guys its 1.30 am and I just can't see where the problem here !
the text file contains just brand , model and price, please help !

#include<iostream>
#include<fstream>

using namespace std;

    struct computer{
    char * brand ;
    char * model;
    double  price;
    };

    void readin(computer * , int );
    void printf(computer * );
    int main ()
  {
    computer * pc ;
    computer * temp;
    int size;
    char filename [30];
    
    cout<< " please enter the file name : "<<endl;
    cin>>filename;
    readin(pc, size);
    printf(pc);
   
    delete [] pc;
    delete [] temp;
    
    system("pause");
    return 0;
}

// reading function 
    void readin(computer * , int )
    {
    computer * pc ;
    computer * temp;
    int size;
    ifstream fin;
    char filename [30];
    fin.open(filename);
    fin>>size;
    
    pc = new computer[size];
    temp = pc;
    for (int i =0; i<size; i++)
    {
    (*temp).brand = new char [15];
    (*temp).model = new char [15];
    
    fin>>(*temp).brand>>(*temp).model>>(*temp).price;
    temp++;
    
    }
    fin.close();
    
    temp = pc;
    for ( int i = 0; i<size; i++)
    {
    delete [] (*temp).brand;
    delete [] (*temp).model;
    temp++;
    
    }
    delete [] pc;
    delete [] temp;
    }
 // printing function 
    void printf(computer * )
    {

    computer * pc ;
    computer * temp;
    int size;

    for(int i= 0; i<size; i++)
    {
    cout<<"brand:"<<(*temp).brand<<"model:"<<(*temp).model<<"price:"<<(*temp).price <<endl;
    temp++;
    }
    temp = pc;
    for ( int i = 0; i<size; i++)
    {
    delete [] (*temp).brand;
    delete [] (*temp).model;
    temp++;
    
    }
    temp = pc;
    
    delete [] pc;
    delete [] temp;


    }

Then,

fin >> size;

fin >> size;
This will try to copy the value of fin, which is an ifstream variable to size. What is the use of that?

I think I know that one. I believe the first line in the file is an integer with the number of entries forthcoming. I do agree with you that the entire program is very confusing.

@OP: There's a lot of excess creation and deletion of variables that is not necessary with the proper passing. Another quick hint for you is that when you have (*temp).something it can be written as temp->something . However in this case I think you may be better off just addressing the elements using the array notation, e.g., pc.price.

hi guys
thanks a lot ...! the program suppose to readin the data from the file and the file look like this :
3
Dell inspiron 399
Hp pavilion 499
IBM lenovo 299

where 3 is the how many elements are in the file, so what I mean with fin>>size; it will go and read 3 and then the next step is to put the size in the memory allocation , which was pc = new computer ;
readin function will read the data from the file, store it !
printf function willl display the results to the screen !
thanks again :)

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.