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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:04:52+00:00 2026-06-05T16:04:52+00:00

Because data from file look like this: line 1 is name (first last), next

  • 0

Because data from file look like this: line 1 is name (first last), next line is score (score1 score 2 ….score5) and so on… So I think that I need getline for name and >> for score

Example of data file

David Beckham
80 90 100 20 50
Ronaldinho Gaucho
99 80 100 20 60
....

First of all, I have structure

struct Player {
string name;
int score[5];
} player[size]

When read data from file

int i = 0;
while(!file.eof())
    {
        for (int j = 0; j < 2; j++) //read each 2 two lines
        {               
            if(j==0) // name
            {               
                getline(file, player[i].name);  
            }
                        else if(j==1) // score
            {
                for(int k=0; k<5; k++) file >> player[i].grade[k];
            }
                }
         i++; //move to next player
    }

Problem is after read all scores (of first player), it seems like doesn’t go to next line to continue read next name, kind of mess up there. So any suggestions to correct my code or new idea to do this?

  • 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-06-05T16:04:53+00:00Added an answer on June 5, 2026 at 4:04 pm

    After reading the last score, the line break is still sitting on the input buffer. You need to skip that. The ignore function is useful for that.

    getline(file, player[i].name);
    for (int k = 0; k < 5; ++k)
      file >> player[i].grade[k];
    file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    

    Check for errors as appropriate. The >> operator, getline, and ignore all return the stream reference, which you can check for success or failure.


    There’s no need for that j loop since each iteration does a completely different thing. Just write the j=0 case immediately followed by the j=1 case, and then get rid of the loop, like my code above. (And note that j will never equal 2 inside the loop, so your condition was wrong anyway.)

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

Sidebar

Related Questions

I'm working on a program which reads some data from a file formatted like
I am getting an error when I run this query because the data type
Using Java sockets, I made a simple server. This works because it sends data
I have a file that looks like this: 1st ­ ­ ­ ­­­ 2nd
Because the data size isn't little that my web app needs to load, it
I am displaying charts that load the data asynchronously because the searches are the
The data is loaded once because it takes a while to load, doesn't change,
I have a set of data that contains garbled text fields because of encoding
I am very interested in postgesql because they have a uuid data type. I
I'm sending form data to an iframe ( <form target=myIframe ... > ) because

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.