Monday, May 24, 2010

C++ data file handling...urgent i have to submit my project...10 pts if u correct the error,?

consider this code...


class stat


{


int played,won,lost;


float win;


public:


stat()


{


played=won=lost=0;


win=0;


}


void update(int a)


{


played++;


if(a==1)


won++;


else


lost++;


win=((float)won/played)*100;


}


void disp()


{


cout%26lt;%26lt;"Played :"%26lt;%26lt;played;


cout%26lt;%26lt;"\nWon :"%26lt;%26lt;won;


cout%26lt;%26lt;"\nLost :"%26lt;%26lt;lost;


cout%26lt;%26lt;"\nWin% :"%26lt;%26lt;setprecision(2)%26lt;%26lt;win;


}


};





void write2file(int a)


{


stat data;


ifstream fin;


fin.open("stat.dat");


fin.read((char*)%26amp;data,sizeof(stat));


data.update(a);


fin.close();


ofstream fin1;


fin1.open("stat.dat");


fin1.write((char*)%26amp;data,sizeof(stat));


}


void dispstat(void)


{


stat data;


ifstream fin;


fin.open("stat.dat");


fin.read((char*)%26amp;data,sizeof(stat));


data.disp();


}


void main()


{


for(int i=0;i%26lt;12;i++)


{


clrscr();


write2file(1);


dispstat();


getch();


} }


after 12 times the values are changing , what could be the possible error and give the correction also.

C++ data file handling...urgent i have to submit my project...10 pts if u correct the error,?
actually, in main, when you call


write2file(1);


the variable declaration for this function is an int, so in main that function call should be:


write2file(int);


gimme those 10 pts





how about this, delete the clrscr(), there is no function declaration for this function in your program, so it might be clearing the values inputted for the win/loss/played.
Reply:The code clearly isn't debugged correctly. What is the program suppose to do
Reply:I see it!





In main(), you say:





write2file(1);





I think you mean to say:





write2file(i);





See it? Very easy to miss. You want to call write2file() with parameter 'i' not one '1'





This is classic programming heartache. Now I know why a lot of guys declare incrementor 'i' like so:





for(int iii=0; iii%26lt;12; iii++)


{


write2file(iii);





...really jumps out at you now, right? No way to confuse '1' with 'iii' ...

phlox

No comments:

Post a Comment