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

  • Home
  • SEARCH
  • 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 3985702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:55:21+00:00 2026-05-20T05:55:21+00:00

I am trying to load some data from a text file into a vector

  • 0

I am trying to load some data from a text file into a vector of structures. My question is, how do you indicate the size of the vector? Or should I be using a the vector push_back function to do this dynamically, and if so, how does this work when populating a structure?

The full program is outlined below:

My structure is defined as

struct employee{
    string name;
    int id;
    double salary;
};

and the text file (data.txt) contains 11 entries in the following format:

Mike Tuff
1005 57889.9

where “Mike Tuff” is the name, “1005” is the id, and “57889.9” is the salary.

I am trying to load the data into a vector of structs using the following code:

#include "Employee.h" //employee structure defined in header file

using namespace std;

vector<employee>emps; //global vector 

// load data into a global vector of employees.
void loadData(string filename)
{
    int i = 0;
    ifstream fileIn;
    fileIn.open(filename.c_str());

    if( ! fileIn )  // if the bool value of fileIn is false
         cout << "The input file did not open.";

    while(fileIn)
    {
        fileIn >> emps[i].name >>emps[i].id >> emps[i].salary ;
        i++;
    }

    return;
}

When I execute this, I get an error that says: “Debug Assertion Failed! Expression: vector subscript out of range.”

  • 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-20T05:55:22+00:00Added an answer on May 20, 2026 at 5:55 am

    A vector is expandable, but only through push_back(), resize() and a few other functions – if you use emps[i] and i is greater than or equal to the size of the vector (which is initially 0), the program will crash (if you’re lucky) or produce weird results. If you know the desired size in advance, you can call e.g. emps.resize(11) or declare it as vector<employee> emps(11);. Otherwise, you should create a temporary employee in the loop, read into it, and pass it to emps.push_back().

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

Sidebar

Related Questions

I'm trying to use the OleDb CSV parser to load some data from a
So I'm trying to load some returned html from an .aspx page but a
In the NodeMouseClick event I'm trying to load some objects from the Node.Tag. Before
I'm trying to call a function after I load some XML into Actionscript, and
I am am trying to load a SQL table from a flat file. The
I am trying to get some data from a database on the fly and
So I'm trying to import some sales data into my MySQL database. The data
I'm trying to get some json data from a remote website. I run my
I have a mysql database that I'm trying to populate from a text file.
In the code below I'm trying to load some images and put them in

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.