Need help!

Yea hello all..

Would like help in spliting up my string which is in a text file.

EG. Name,Class,Classroom,etc

How would i be able to print lets say, classroom?
Last edited on
Really depends on how you've stored the data.

If you provide some sample code, it'd be a lot easier to advise.
hi
you probably used fstream library and saved in
ofstream save ("something.txt");

and than
save<< Name<<' '<<classroom; //and so on


now you have it in your txt file and all you need to do is make another function where you return whatever you need.
ifstream save ("something.txt");
save>> Name>> classroom; //and so on
return Name; // or classroom;


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
void CFunct::avgSal()
{

	ifstream file ("file.txt");

	int found;
	int count = 0;
	float sum = 0;
	float salary = 0;
	float avg = 0;
	string line;
	float tempdip = CLect::salary;

	found = CPerson::type.find(tempdip);

	while (!file.eof())
	{	
		getline(file,line);

		if(found = line.find(tempdip, 0) != line.npos)
		{
			
			file >> CLect::salary;
			sum += CLect::salary;
			count ++;

		}
	}

	cout << sum << endl;

	cout << "Average Salary: " << salary << endl;


	file.close();
};


Right now this function would be incomplete, but mainly i am stuck with how i would skip the commas in the text file and take out a certain part of the line

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
void CFunct::insertlec()
{
	CPerson::getinfo();

	CPerson::type = "Lecturer";

	cout << "Room: ";
	cin >> CLect::room;

	cout << "Salary: ";
	cin >> CLect::salary;

	ofstream file ("file.txt",ios::app);
	
	file << CPerson::type << ",";
	file << CPerson::name << ",";
	file << CPerson::nric << ",";
	file << CLect::room << ",";
	file << CLect::salary << ",";
	file << CPerson::year << ",";
	file << CPerson::month << ",";
	file << CPerson::day << endl;
	
	file.close();

};


this is how i store the data in the txt file.
Last edited on
@mirec

Yea but thats the problem, i have to for example "Name,class,classroom" i would only need to print out "class" but i have no idea how to skip the commas in the line and only print out that certain part of the line
Topic archived. No new replies allowed.