Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:53:01+00:00 2026-05-26T11:53:01+00:00

For each line of text: -Get first name -Get last name -Get Student Number

  • 0

For each line of text:

-Get first name
-Get last name
-Get Student Number

(There will be an unknown amount of lines, each looking the same in format.)
Format:

First Last 111111111
First Last 111111112
...

I want to put each piece of each student into its own struct. I’ve set up the struct as follows:

struct Student{  
    string lastName;
    string firstName;
    string stdNumber;
    double assgn1;//doubles will be used later in prog.
    double assign2;
    double assign3;
    double assign4;
    double midTerm;
    double finalGrade;
};

My file input function is as follows:

int getFileInfo()
{       
    int failed=0;
    ifstream fin;   
    string fileName;    
    vector<Student> students;

    Student s;                  // A place to store data of one student
    cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
    do{                                 
        if(failed>=1)                           
            cout<<"Please enter a correct filename."<<endl; 
        cin>>fileName;                              
        fin.open(fileName.c_str());// Open the file     
        failed++;                                           
    }while(!fin.good());                                        
    while (fin >> s.firstName >> s.lastName >> s.stdNumber)
        students.push_back(s);                      
    fin.close();                            
    return 0;                                   
}  

In the file input, I make a vector, but I am unsure how to access each individual part of it, or the student, s, because it seems that it only makes one student. I was told that each line of the file is split and inputted into the students vector, but I don’t know how to extract that information. How do I get each student out of the students vector and into its own struct so I can use each student as its own struct?
So in the end, I would want to be able to output:

Student1: First Last 111111111
Student2: First Last 111111112
However More students are in the file

Thanks in advance for the help!!

@Loki
I changed the loop so loop != end and I still get the same problem. Here is my code:

int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;// A place to store the list of students
vector<Student>::iterator loop = students.begin();
   vector<Student>::iterator end  = students.end();

Student s;                  // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
    cout<<"Please enter a correct filename."<<endl;
    cin>>fileName;
    fin.open(fileName.c_str());// Open the file
    failed++;
}while(!fin.good());

 while (fin >> s.firstName >> s.lastName >> s.stdNumber){
    cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl;
    students.push_back(s);
}
fin.close();

    for(loop;loop!=end;++loop)
    cout<<loop->firstName<<" "<<loop->lastName<<" "<<loop->stdNumber<<endl;

return 0;
}

Again, Visual studio is saying vector iterators incompatible,
Pointing to “C:\Program Files(x86)\Microsoft Visual Studio 10.0\vc\include\vector”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:53:02+00:00Added an answer on May 26, 2026 at 11:53 am

    In the file input, I make a vector, but I am unsure how to access each individual part of it.

    You can access individual elements via the operator[]

    students[0].firstName;  // get the first name.
    
    std::cout << students[1].firstName; // prints out the first name.
    

    You can use an index from 0 -> size().

    size_t  size = students.size();  // number of students in the vector.
    
    students[size].firstName; // This is wrong. You expression in
                              // [] must be less than size as the elements are
                              // numbered from 0 to size() -1.
    

    because it seems that it only makes one student.

    You are creating only one student ‘s’ (As a side not pick a better name). But when you call push_back() you are copying this student object into the vector. So the vector gets a copy of ‘s’ that it stores. Then each time through the loop you overwrite the old values in ‘s’ with new values retrieved from the file.

    I was told that each line of the file is split and inputted into the students vector, but I don’t know how to extract that information.

    As noted above each element can be reached individually using the operator[]

    Alternatively you can use iterators to get a range. of students.

     std::vector<Student>::iterator loop = students.begin();
     std::vector<Student>::iterator end  = students.end();
    

    You can access an element by de-referencing loop and move to the next student by incrementing loop.

     std::cout << loop->firstName;   // prints out student[0]
     ++loop;                         // Increment loop now it points at student 1
     std::cout << loop->firstName;   // prints out student[1]
     ++loop;                         // Increment loop now it points at student 2
     // etc
    

    If loop == end then you have moved past all the students.

    How do I get each student out of the students vector and into its own struct so I can use each student as its own struct?

    The students are in the vector already in structs. you can use them directly as you would a struct outside. But if you want to copy them out you can do this:

    Student   tmp = students[0]; // copies student 0 into tmp.
    

    I would want to be able to output:

    Use std::cout to output stuff to the standard output. Or create an object of type std::ofstream to output to a file:

    std::cout << tmp.lastName  << " " 
              << tmp.firstName << " "
              << tmp.stdNumber << "\n";
    
    std::ofstream  file("plop1.txt");
    file      << tmp.lastName  << " " 
              << tmp.firstName << " "
              << tmp.stdNumber << "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In php how can I read a text file and get each line into
How do I remove the last two chars from each line in a text
I'm trying to read the last line from a text file. Each line starts
I've a text file with 2 million lines. Each line has some transaction information.
I have a text file with the following: First Name : Javier Last Name
I have a class that opens text files to grab first name, last name,
I'm trying to get each element on each line in a text file so
I'm trying to parse a text file. First I plan on extrcating each line,
I have some text where each line of text has some good words and
I want to run a bash command once for each line of a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.