ifstream / ofstream Problem

Ok, I have a small game I am making, and I used a old save/load system I found on these forums. But unfortunately, it doesn't work. It saves fine, but it doesn't load and re assign the integers. I only want it to load the integers from the file, no strings or any other data type. Here is my code: (sorry for posting all of it)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Dice_1.cpp : Defines the entry point for the console application.
//

//ans used for root, ans1 used for SP, ans2 used for MP, ans3 used for options

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <fstream>
#include <string>


using namespace std;

int main()
{
	string dev;
	string quit;
	bool running = false;
	bool first_time = true;
	double ratio = 0.0;//new
	int ans, ans1, ans2, ans3, random, random1;
	int spd = 1;
	int mpd = 1;
	int total = 0;
	int snake = 0;
	int roll = 0; //new
	int bet = 0;
	int money = 500;//new
	double win = 0.0;//new
	double lose = 0.0;//new
	srand(time(NULL));
	
	cout<<"1. SinglePlayer"<<endl;
	cout<<"2. Multiplayer"<<endl;
	cout<<"3. Quit"<<endl;
	cin >> ans;

	

		if (ans1 == 3){
			system("cls");
			cout<<"Saving File..."<<endl;
			ofstream dice_Save;
			dice_Save.open("dice_Save.txt");
			dice_Save <<"RUN: "<<running<<endl;//1
			dice_Save <<"FIRST: "<<first_time<<endl;//2
			dice_Save <<"TOTAL: "<<total<<endl;//3
			dice_Save <<"SNAKE: "<<snake<<endl;//4
			dice_Save <<"SPD: "<<spd<<endl;//5
			dice_Save <<"MPD: "<<mpd<<endl;//6
			dice_Save <<"GP: "<<money<<endl;//7
			dice_Save <<"WIN: "<<win<<endl;//8
			dice_Save <<"LOSE "<<lose<<endl;//9
			dice_Save <<"RATIO: "<<ratio<<endl;//10
			dice_Save.close();
			cout<<"Save Complete"<<endl;
			cout<<"Do you want to quit now?: ";
			cin >> quit;

			if (quit == "Yes" || quit == "yes"){
				cout<<"Thanks for Playing!"<<endl;
				cin.get();
				cin.get();
				return 0;
			}

			else {
				goto menu;
			}
		}

		if (ans1 == 4){
			system("cls");
			cout<<"Load File (NOTE: WILL LOAD MOST RECENT SAVE)"<<endl;
			string junk;
			ifstream dice_Save ("dice_Save.txt", ifstream::in);
			
			getline(dice_Save, junk);
			dice_Save >> junk;
			dice_Save >> running;//1
			dice_Save >> junk;
			dice_Save >> first_time;//2
			dice_Save >> junk;
			dice_Save >> total;//3
			dice_Save >> junk;
			dice_Save >> snake;//4
			dice_Save >> junk;
			dice_Save >> spd;//5
			dice_Save >> junk;
			dice_Save >> mpd;//6
			dice_Save >> junk;
			dice_Save >> money;//7
			dice_Save >> junk;
			dice_Save >> win;//8
			dice_Save >> junk;
			dice_Save >> lose;//9
			dice_Save >> junk;
			dice_Save >> ratio;//10
			cout<<"Load Succesfull"<<endl;
			cin.get();
			cin.get();
			goto menu;

			
		}

		
Last edited on
Please do not just say it does not work, give us line numbers, and specific areas of code that are not doing what they are supposed to be.
Ok sorry I edited the code to show only the variables and save/load system. Although I did say in my initial post that the load system didn't work, specifically it doesn't load any of the integers and assign them correctly into the program. I'm asking for help on how to fix it.
It might have something to do with the fact that you never assign a value to ans1. Isn't your compiler giving you any warnings or errors?

Also, goto/labels are a bad bad thing. Try to refrain from using them. Instead, use loops.
Sorry I don't think you understood the question... My load system isn't working, ans1 isn't part of of the load function and it works fine, I was looking for help on how to load integers from a file. And yes, I know goto is bad but I honestly don't care. All this code is actually in loops I was just being lazy for testing purposes.
I completely understood your question. I don't think you understand your code.

Here:
1
2
3
4
out<<"1. SinglePlayer"<<endl;
	cout<<"2. Multiplayer"<<endl;
	cout<<"3. Quit"<<endl;
	cin >> ans;


And then here:
if (ans1 == 3){ if (ans1 == 4){

Where do you assign ans1? I don't understand your code at all, and I believe that's a good indicator that you don't either.

1.-You used label "menu" with goto, but didn't define it.

2.-You lack a } at the end of the program.

3.-What Volatile Pulse says. Seems like if the menu is incomplete, and the if/elses would be in a singleplayer save menu which lacks where to get there from.

4.-I would remove the name of the information you put in the text file, maybe it's reading the name instead of the value, try:
dice_Save <<running<<endl;
Instead of:
dice_Save <<"RUN: "<<running<<endl;
Last edited on
Ok I see what you guys are saying, the first poster asked that I only give the code you needed. I don't have access to my computer so I just used my iPhone to edit the message and I didn't put the code in separate tags. I have about 200 more lines of code that actually go with this program. The if (ans1 == x) works fine and that's how I learned how to do it. Again, I only gave you the code you need.
Ok, then:

4.-I would remove the name of the information you put in the text file, maybe it's reading the name instead of the value, try:
dice_Save <<running<<endl;
Instead of:
dice_Save <<"RUN: "<<running<<endl;


Last edited on
Thanks for being patient and sorry for the horrible explanation! I'll try that thanks.
Topic archived. No new replies allowed.