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 6794409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:07+00:00 2026-05-26T18:13:07+00:00

I am having some trouble with my lab assignment for my CMPT class… I

  • 0

I am having some trouble with my lab assignment for my CMPT class…
I am trying to read a text file that has two words and a string of numbers per line, and the file can be as long as anyone makes it.

An example is

Xiao Wang 135798642
Lucie Chan 122344566
Rich Morlan 123456789
Amir Khan 975312468
Pierre Guertin 533665789
Marie Tye 987654321

I have to make each line a separate “student”, so I was thinking of using struct to do so, but I don’t know how to do that as I need the first, last, and ID number to be separate.

struct Student{
    string firstName;
    string secondName;
    string idNumber;
};

All of the tries done to read in each word separately have failed (ended up reading the whole line instead) and I am getting mildly frustrated.

With the help from @Sylence I have managed to read in each line separately. I am still confused with how to split the lines by the whitespace though. Is there a split function in ifstream?
Sylence, is ‘parts’ going to be an array? I saw you had indexes in []’s.
What exactly does the students.add( stud ) do?
My code so far is:

int getFileInfo()
{
    Student stdnt;
    ifstream stdntFile;
    string fileName;
    char buffer[256];
    cout<<"Please enter the filename of the file";
    cin>>filename;
    stdntFile.open(fileName.c_str());
    while(!stdFile.eof())
    {
        stdFile.getLine(buffer,100);
    }
    return 0;
}

This is my modified and final version of getFileInfo(), thank you Shahbaz, for the easy and quick way to read in the data.

void getFileInfo()
{
    int failed=0;
    ifstream fin;
    string fileName;
    vector<Student> students; // A place to store the list of 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();
cout<<students.max_size()<<endl<< students.size()<<endl<<students.capacity()<<endl;


return;
}

What I am confused about now is how to access the data that was inputted! I know it was put into a vector, but How to I go about accessing the individual spaces in the vector, and how exactly is the inputted data stored in the vector? If I try to cout a spot of the vector, I get an error because Visual Studio doesn’t know what to output I guess..

  • 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-26T18:13:07+00:00Added an answer on May 26, 2026 at 6:13 pm

    The other answers are good, but they look a bit complicated. You can do it simply by:

    vector<Student> students;   // A place to store the list of students
    
    Student s;                  // A place to store data of one student
    
    ifstream fin("filename");   // Open the file
    
    while (fin >> s.firstName >> s.secondName >> s.idNumber)
        students.push_back(s);
    

    Note that if istream fails, such as when the file finishes, the istream object (fin) will evaluate to false. Therefore while (fin >> ....) will stop when the file finishes.

    P.S. Don’t forget to check if the file is opened or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I having some trouble with an APCS assignment. The program is supposed to read
I'm having some trouble trying to animate some text around a circular path in
Hey having some trouble trying to maintain transparency on a png when i create
I´m having some trouble retrieving a collection of strings in a projection: say that
Having some trouble with the CSS alignment of text generated with @font-face. For some
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
Im having some trouble looping through some xml data. The xml file is structured
Been having some trouble trying to integrate the admob sdk into my application to
I having some trouble trying to figure out how to implement a Rails 3
Having some trouble passing values from one class to another. I basically have a

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.