something wrong!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void kbc::sce()   // member function of class kbc
{ clrscr();
ifstream ifs("SCORE.TXT");  // a file stores score after game is played
if(ifs.eof())                // checking whether any score stored and printing 
{ gotoxy(15,15);
  cputs(" No score registered \n");
  getch();
}
else                                
if(!ifs.eof())
{while(!ifs.eof())
{ ifs.read((char*) &t,sizeof(t));  // 't' is object of class kbc
       if(ifs.eof())
	break;
       t.score();
}
} getch();
ifs.close();
}


This is working for infinite times unless any record is available for it to be read.
btw, i tried this code just for test the link you gave on the above post:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
using namespace std;

int main () {
	int i = 0;
	
	cout << "enter: ";
	while (cin >> i) {
		cout << "you've entered: " << i << endl;
		cout << "enter again (non-integer type to stop): ";
	}
	cout << "bye2..." << endl;
	cin >> i;
	return 0;
}


but, why the
1
2
	cout << "bye2..." << endl;
	cin >> i;

part don't work properly?

btw to the OP:

1
2
3
4
5
6
7
if(!ifs.eof())
{while(!ifs.eof())
{ ifs.read((char*) &t,sizeof(t));  // 't' is object of class kbc
       if(ifs.eof())
	break;
       t.score();
}


why not?
1
2
3
4
while (!ifs.eof()) {
    ifs.read((char*) &t,sizeof(t));  // 't' is object of class kbc
    t.score();
}
Last edited on
Because the stream gets an invalid state.
Besides, you wouldn't got out of the loop if you could keep reading.
@ne: so how to resetting the stream back to normal? i tried to use cin.clear() but seems like it doesn't working...
@chipp
I like how you have stolen this thread ;)
Be more specific what you mean by "back to normal". cin.clear() will clear the error flags of the stream so that you can read from it again.
Last edited on
sorry i am not able to resolve any error. by any above methods

@chipp
I like how you have stolen this thread ;)
Be more specific what you mean by "back to normal". cin.clear() will clear the error flags of the stream so that you can read from it again.

hahaha... sorry dude, don't realized that i hijacked someone's thread...

@OP: excuse me bro...

@peter: what i mean is clear the input buffer of the stream... any links?
Because you didn't read the input. It remains in the buffer.
You need to discard it std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Topic archived. No new replies allowed.