Reading many lines and separating by spaces on each line

Hey guys so i think all of my code is working except for reading my text file input. The text files will always be formatted with two names on a line, first and last name. And the next name will always come on the next line. I need to input a line, input the first word as the first name and the next as the last name. I've been trying but what i have just isn't working. Any thoughts? Thanks
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
 #pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "ClassSection.h"
#include "ClassSection.h"
#include <sstream>
using namespace std;
class Student
{
public:
	string firstName;
	string lastname;
	int numSkips;
	int numExceptional;
	int numSatisfactory;
	int numUnsatisfactory;

};
class ClassSection
{
public:
	string className;
	Student student[50];

};

int main()
{
	ClassSection section1;
	//Student students[50];
	cout << "Please enter your class name: ";
	cin >> section1.className;
	string filename, studentname, line;
	cout << "Please enter your filename: ";
	cin >> filename;
	int x = 0;
	fstream MyReadFile(filename);
	while (getline(MyReadFile, line))
	{
		string data;
		stringstream linestream(line);

		getline(linestream, data, '\t');

		linestream >> section1.student[x].firstName >> section1.student[x].lastname;
	
		cout << section1.student[x].firstName << section1.student[x].lastname;
		x++;
	}
What do you mean by "not working" ?
Sorry for double post.

I think the error come from line 44.

If it's not the case, use MyReadFile.is_open() to check if you can read your file.
Everything compiles and runs but during execution the array isn't even being filled from what i understand. I am definitely opening the file but my guess is there is an error somewhere in line 39-46 but i'm not sure what is going wrong, not very well versed in the stringstream stuff.
What I would do instead of your while is the following:
1
2
3
4
5
std::ifstream myFile(filename);
while (myFile >> firstN) {
  myFile >> lastN;
  // then do what you want with it
}


If I am not saying bullshit, it should works.
The text files will always be formatted with two names on a line, first and last name. And the next name will always come on the next line. I need to input a line, input the first word as the first name and the next as the last name.


Assuming that first and last names don't contain a white-space char, then the easiest way to read them is:

1
2
3
4
5
6
fstream MyReadFile(filename);

if (MyReadFile)
     for (int x = 0; MyReadFile >> section1.student[x].firstName >> section1.student[x].lastname; ++x);
else
    cout << "Problem opening file\n";

> getline(linestream, data, '\t');
you never mentioned a tab in the input, ¿why don't you show an example of your input file?

> the array isn't even being filled from what i understand
at each iteration of the loop
- ¿what does `line' contain? ¿is that correct?
- ¿what do `firstname' and `lastname' contain?
once the loop is finished, ¿what does the `section1.student' array contain? ¿how are you checking that?


if you didn't realise at this point, we can't see your screen
Topic archived. No new replies allowed.