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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:12:49+00:00 2026-05-30T22:12:49+00:00

i am experiencing much trouble reading in files from input into an array struct.

  • 0

i am experiencing much trouble reading in files from input into an array struct. here is the code if someone can tell me what im doing wrong i can figure it out. the loop is supposed to be reading 2 strings, and 1 int, and skipping possible blank lines. but when i run it, it reads the first set and doesnt read nothing after that.

struct Instruments 
{
string model;
string maker;
int year;
};

int main()
{
int size;
Instruments data[20];
int i =0;

ifstream fin;
fin.open("input.txt");

for (i=0; i<20; i++)
{
 do{
getline(fin, data[size].model);
    getline (fin, data[size].maker);
fin >> data[size].year;

    size++;
 }
 while (data[size].model.length() > 0);
 }

fin.close();

for(int i=0;i<size; i++)
{
        cout << data[i].model << "model"<<endl;
        cout << data[i].maker << "maker" << endl;
        cout << data[i].year<< " year" << endl;
}
return 0;
}
  • 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-30T22:12:50+00:00Added an answer on May 30, 2026 at 10:12 pm

    There are multiple issues here:

    1. Your first ‘for’ loop is using i as the loop counter but size as the array index.

    2. After this call:

      fin >> data[size].year

      it will read to the end of the number and any whitespace that follows will form part of your next read, so if you are expecting to start the next record at the next line, do a blank getline() here too.

    Aside from that.

    1. Use vectors not arrays
    2. Have a method to read from a stream into your struct, and if that succeeds, use push_back() to add it to your vector.

    That doesn’t necessarily mean you have to loop until the read fails, it may be that you know in advance how many you wish to read. But you should still do it this way.

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

Sidebar

Related Questions

I don't know why I'm experiencing so much trouble with this, but I would
I am moving a project which uses clapack from osx to linux and experiencing
Just updating my code to be Rails 3 ready, however I'm experiencing a major
Currently I am experiencing a frustrating bug in my code. It seems that I
I am experiencing difficulty with the following VBS code. It works only sometimes, and
I'm trying to convert some code from Python to C++ in an effort to
I'm experiencing trouble upgrading my rails 2.3.14 / ruby 1.8.7 app to 3.1.1/1.9.2 :
I am experiencing a memory leak with a code similar to the one below
I just recently move a site from IIS6 to IIS7 and are experiencing a
I'm diving into Ruby on Rails and I'm experiencing a weird bug, using Rails

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.