Hello
I had the seg fault last night, after making some changes, I don't have it anymore, the program compiles but it doesn't do anything !
" the program should read in data from the file in this following form :
2
Dell inspiron 299
Hp pavilion 499
"
simply read in and the display ! thanks in advance
#include<iostream>
#include<fstream>
using namespace std;
struct computer{
char * brand ;
char * model;
double price;
};
void readin(computer * , int );
void printf(computer * , int );
int main ()
{
computer * pc ;
computer * temp;
int size;
char filename [30];
ifstream fin;
cout<< " enter the file name : "<<endl;
cin>>filename;
fin.open(filename);
readin(pc, size);// call the read in function
for(int i = 0; i<size; i++)
{
printf(pc, size);// call to the print function to display the results
pc++;
}
// dealllocate the memory
delete [] pc;
delete [] temp;
system("pause");
return 0;
}
// functions implementation
// reading function
void readin(computer * pc , int size)
{
computer * temp;
ifstream fin;
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();
// set the pointer back to base
temp = pc;
// deallocate the memory
for ( int i = 0; i<size; i++)
{
delete [] (*temp).brand;
delete [] (*temp).model;
temp++;
}
delete [] pc;
delete [] temp;
}
// printing function
void printf(computer * pc, int size )
{
computer * temp;
for(int i= 0; i<size; i++)
{
cout<<"brand:"<<(*temp).brand<<"model:"<<(*temp).model<<"price:"<<(*temp).price <<endl;
temp++;
}
temp = pc;
//decallocate the memory
for ( int i = 0; i<size; i++)
{
delete [] (*temp).brand;
delete [] (*temp).model;
temp++;
}
//set it back to base ;
temp = pc;
// decallocate the memory
delete [] pc;
delete [] temp;
}