Reading First and Last Name from File

everything works when I try to read from the file when I remove the Jr. from the name . How can I read the last name as Smith Jr. using this input file? I can't use get line because that reads the whole line as the full name but how can I read the name and last name separate with last name being Smith Jr. instead of causing errors?

1
2
3
4
5
6
7
8
9
Intro To Computer Science c++
SAL 343
JHG 344
John Adams
111223333 100 87 93 90 90 89 91 
Willy Smith
222114444 90 73 67 -77 70 80 90 
Phil Jordan
777886666 100 80 70 -50 60 90 100


When I remove Jr. from the input file it reads the last name Smith but when I place it as Smith Jr. it doesn't read it as last name and causes huge printing errors. How can I read the last name as Smith Jr. instead of removing it? thank you here's my reading function

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
void Read_Student(Student & Temp , ifstream & fin){

    fin >> Temp.FirstName >> Temp.LastName ;
    fin >> Temp.Id;
    fin >> Temp.Quiz;
    for (int i = 0 ; i < 6 ; i++)
        fin >> Temp.Test[i];
    fin.ignore(10,'\n');
}
int Read_List(Student & Temp,Student List[], int Max_Size)
{
    ifstream fin;
    int i = 0;

    if (Open_File(fin))
    {
        getline(fin, List[i].Course_Name);
        getline(fin, List[i].Course_Id);
        getline(fin, List[i].Course_Location);
        Read_Student(List[i],fin);
        while(!fin.eof())
        {
            i++ ;
            if (i > Max_Size){
                cout << "\nArray is full.\n" ;
                break;
            }
            Read_Student(List[i],fin);
        }
    }
    else
    {
        cout <<"\nBad file. Did not open. Program terminated.\n";
        exit(0);
    }
    return (i);
}


please help i've tried everything.. get line doesn't work and it reads Id instead of last name..
please help!
For the name, you could use the input the way you already have, but instead read in the last name with a getline, using a . as the third parameter, which tells the getline to stop reading input when a . is encountered.

1
2
fin >> Temp.FirstName;
getline(fin, Temp.LastName, '.');
It works only for the names that have for ex. Jr. what if a name just had first and last without for ex Jr. but other than that it doesn't.. any advice i've been trying forever
Topic archived. No new replies allowed.