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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:34:21+00:00 2026-06-04T04:34:21+00:00

Our professor gave us this assignment, where we have a .txt file with the

  • 0

Our professor gave us this assignment, where we have a .txt file with the following format:

John 23
Mary 56
Kyle 99
Gary 100
...etc. etc.

What we have to do is read the file, and store the names and scores in parallel arrays.

This is turning out to be a bit more challenging to me than I anticipated. What is confusing me, when searching around stack, is all the different libraries people use to do this. Our Prof just wants us to use string, fstream, and sstream to do this.

Below is what I’ve come up with so far, it compiles perfectly, splits the scores from the names but stores them in the same array:

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  const int SIZE = 50;
  string names[SIZE];
  int score [SIZE];
  short loop = 0;
  string line;

  ifstream inFile("winners.txt");

  if (inFile.is_open())
  {
    while(!inFile.eof())
    {
       istream& getline(inFile >> line);
       names[loop] = line;
       cout << names[loop] << endl;
       loop++;
    }
    inFile.close();
  }

  else cout << "Can't open the file" << endl;

  return 0;
}

I’m not looking for someone to solve my HW problem, I just want a push in the right direction!

  • 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-04T04:34:22+00:00Added an answer on June 4, 2026 at 4:34 am

    If you want to read two things for each line of input, it seems reasonable to have two “read” statements:

    std::string name;
    inFile >> name;
    
    int score;
    inFile >> score;
    
    std::cout << "Read score " << score << " for name " << name << '\n';
    

    …then you can do that repeatedly until you’ve read the entire file.


    Edit: After you get the basic logic worked out, you might want to think about error handling. For example, what is appropriate behavior for your program if the input file doesn’t contain 50 pairs of (name, score)? How can you change your code to get that behavior?

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

Sidebar

Related Questions

For a class assignment, I'm required to write an object to a file. Our
I was given some code by our professor with some things I have never
OK, our professor explained (kinda) this problem, but it still doesn't make much sense.
In my work with my professor I have to ssh into our server and
This is part of a homework assignment. What we have to do is write
Our professor is making us do some basic programming with Java, he gave a
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have
Our company produces cross-platform software and we have Bamboo instance which is building projects
Our Django project is getting huge. We have hundreds of apps and use a
I've taken 2 OOP C# classes, but now our professor is switching over to

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.