function not reading file contents correctly

Can someone please explain how to fix the glitch i am experiencing? i dont understand why its occurring however i am fairly new to c++.

The function is supposed read back each line of a .txt file and store the value of the line into a string "D", and then the data of that string is then assigned to integers. then set the string back to have no value then its supposed to go read the next line.

however after the file is loaded i check the values of the integers and every single one is shown as being 0. i have verified my file being read does have the right have the correct values in it which are greater then 0. the glitch the load function is not reading those values right and i have no clue why.
i have the code typed right according to the references i have checked on this website.

everything other then it not reading the values in the file right works correctly for me how i want it to.

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
#include "Lib.h"

#include <fstream>
using namespace std;

#include <sstream>
using namespace std;

string D;

void Load()
{
	//figuring out what file to load
	cout << "\n\nWhat Is The Name Of the Character You Would Like to Play?";
	cin >> Name;

	cout << "\n\nWhat Save File Of " << Name << " Would You Like To Play?";
	cin >> choice;

	cout << "\n\n";

	A << choice;
	B = A.str();
	C = ".txt";

	filename = Name + B + C;

	ifstream inputFile;
	inputFile.open(filename.c_str());

	cout << "Loading is at 0 %";

	//Assigning the values stored back to the character
//We ignore this first line of the file because it is just the name of the character which the player just input a few seconds ago
	istream& getline ( istream& inputfile, string& D );;

	//the 2nd line is the amount of times the character was saved
	istream& getline ( istream& inputfile, string& D );
	Saved = 0 + 0 + atoi(D.c_str());
	D = "";
}


this is just a snippet from the function. i did not put the whole function because its around 460 lines and reads back over 90 integers and also because i did notice every single integer got assigned 0 as a value. they are all typed the exact same way which is
1
2
3
istream& getline ( istream& inputfile, string& D );
	Saved = 0 + 0 + atoi(D.c_str());
	D = "";
except the name of the integer gets changed each time
Last edited on
Do you know that lines 35 and 38 do nothing? You're declaring a function, not calling it.
closed account (zb0S216C)
Why include the standard namespace twice?
@helio no i actualy didnt know they did nothing i just entered it how the reference i read for it had it

@Framework im pretty new to c++ and i just listed the namespace under each. my compiler doesnt complain about it.
The code you copied from that reference you use is just code that shows the declaration of those functions, it's not how you should actually be using the functions. Function usages are generally in examples, or you can deduce it yourself if you understand what you are doing.
@LB
So i need the which code below then to assign the Data read from the input file to the string D?

 
getline (inputfile,D);


 
 cin.getline (title);

Last edited on
Topic archived. No new replies allowed.