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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:28:12+00:00 2026-05-20T06:28:12+00:00

I have to load three arrays from a text file for homework and I

  • 0

I have to load three arrays from a text file for homework and I can’t figure out why it isn’t working. This is what the text file look like:

Jean Rousseau  
1001 15.50
Steve Woolston
1002 1423.20
Michele Rousseau
1005 52.75
Pete McBride
1007 500.32

It’s a name on one line, then an id number and a balance due on the next line separated by a space.

This is my function to import the data:

void InputFromFile(string fileName, int sizes, string namesAr[],
int idsAr[], float balancesAr[])
{
// Variables
int indexCount;
ifstream inFile;

// Initialize
indexCount = 0;

inFile.open(fileName.c_str());

while(inFile && indexCount < sizes)
{
    getline(inFile, namesAr[indexCount]);
    inFile >> idsAr[indexCount];
    inFile.ignore(1000, '\n');
    inFile >> balancesAr[indexCount];
    inFile.ignore(1000, '\n');
    indexCount++;
}

inFile.close();
}

This is what gets added to the array when I output all the items…

Jean Rousseau
1001
0

-1
3.76467e-039

36
3.76457e-039

0
6.57115e-039

7736952
8.40779e-045

7736952
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-20T06:28:13+00:00Added an answer on May 20, 2026 at 6:28 am
    inFile >> idsAr[indexCount];
    inFile.ignore(1000, '\n');
    

    You read the first number from the line, then you ignore all the characters until the end of the line, including the second number. You then try to read the next name as if it were a number, and since you aren’t doing any error checking, everything goes wrong.

    This ignore is entirely unnecessary: there is nothing to ignore between the first and second numbers as the >> will skip over any leading whitespace. The second ignore is necessary, since you have to skip the newline at the end of the numbers line before using getline to read the next name line. It may be easier for you to use only getline to read data from the file, then parse the numbers line using a stringstream. Mixing formatted extraction (>>) with line-based extraction can be difficult to get right.

    After any input operation, you must check the state of the stream to ensure that an error did not occur. As a simple example:

    if (!(inFile >> idsAr[indexCount]) {
        /* input failed; handle error as appropriate */
    }
    

    A stream has a number of state flags and when an extraction fails, the fail flag is set and must be cleared before you continue using the stream.

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

Sidebar

Related Questions

I have to load an array of doubles from a file, multiply each element
I have this script to output everything from table called Articles. If I load
This is a method I'm using to load saved game data from a file.
I have a load of objects with arrays in them. var tabData0: Object =
What I want to do is: Load values for a program from a file.
Hi I'm coming from C# and have been working with c++ alot lately and
I have three PHP arrays that I've encoded with json... extra PHP code has
In Excel 2010 I want to load all text files data from a folder
I am trying to load three different images from an array into corresponding cells
This might sound odd, but my issue is that I have a text string

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.