file read/write issues.

I am writing a code for a game, and I want the game to store a win/loss record in a text file... but it isn't working the way I want it to. It pulls the same random integer instead of reading from the text file, then writes that integer to the text file, but then does not read it from the text file when the instance is run again. Can I get some help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 void writeRecord(int p, int d) {
	int wins, losses;
	ifstream fin;
	ofstream fout;
	fin.open("devilsDiceRecord.txt");
	fout.open("devilsDiceRecord.txt");
	
	if (p >= 100) {
		fin<< wins;			//wont read and right as desired... doesn't go in or out to file
		fin<< losses;
		wins++;
		cout << wins << " - " << losses << endl;
		fout << wins << endl;
		fout << losses << endl;
	}
	if (d >= 100) {
		fin >> wins;
		fin >> losses;
		losses++;
		cout << wins << " - " << losses << endl;
		fout << wins << endl;
		fout << losses << endl;
	}
	fin.close();
	fout.close();

	return;
}
I would suggest something along the lines of...

main{

iv=readFile(int); p=iv // (iv is for input value)
d=readFile(int); d=iv

play game

writeFile(p,d);

return 0;
}

int function readFile(int){
open, read, close
return what you read
}

void function writefile(int,int){
open,write,close
}


In this way you only have the file opened for reading, or opened for writing. Not both at the same time. You can track your error down to a function and not "Somewhere" in the haystack...
Last edited on
lines 9-10: I presume you want to READ fin here. If so, you have the wrong operator. It should be fin >>.

lines 9,10,17,18: You're not checking to see if the fin >> operation was successful.
If the operation failed, wins and losses (which are uninitialized variables) won;t be changed.

I caught that mistake, changed it. Still wont work.
Topic archived. No new replies allowed.