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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:39:26+00:00 2026-06-01T21:39:26+00:00

This is a beginner C++ homework problem, I know about vector, but it is

  • 0

This is a beginner C++ homework problem, I know about vector, but it is not allowed to use in this assigment. I searched around and I found how to make an array[] of unknown size too. But it is slightly trickly (sort of). The example I read are all along the lines of: get an input, then create the array base on that input (size n).

I have created this student class, but now I need an array to store them. I do not know in advance how many students there are going to be. I only have an input of text file with each student on one line. The format is below, the sample is about 5, but I don’t know how many the teacher will use to test my program.

Input file like this:

George Chan Computer_Science New York 23
Johnny Chan Computer_Science New Jersery 22
....

But the problem is I don’t know how many students are there until I finish reading the whole text tile (or I don’t know if there are other way to do it). My current approach is to read it line by line, increment a counter studentCounter, until I finish, then I create my Student* myRoster = Student[studentCounter]. But then when I have to start reading the file again. And go line by line to parse the information and create a Student object and let a myRoster[x] points to it. I feel like I am sort of doing the work twice just to have the right size. Are there some tricks or things I am missing?

Thank you.

  • 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-01T21:39:28+00:00Added an answer on June 1, 2026 at 9:39 pm

    Here is one simple approach using recursion:

    Student* readStudents(istream& infile,int& n_students)
    {
      string line = readLine(infile);
      if (!infile) {
        return new Student[n_students];
      }
      int index = n_students++;
      Student* students = readStudents(infile,n_students);
      students[index] = parseLine(line);
      return students;
    }
    

    Then you call it like this:

    int n_students = 0;
    Student* students = readStudents(infile,n_students);
    

    The idea is just to read the lines as you are going deeper into the recursion. By the time you hit the end of the file, you know how many students to allocate, and then you can parse the lines and fill the array in reverse order as you come out of the recursion. You’ll also get the count of students returned through the output parameter.

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

Sidebar

Related Questions

This is NOT a homework. I'm a beginner in programming, and also this is
I know this is a beginner's question, but I've been searching online and all
This is not homework. I am a beginner (novice) java programmer, trying to read
I have this homework to do in C. I'm beginner so it is probably
I'm getting a weird problem in GlassFish 2.1 - I'm a beginner with this
This may be a beginner xml question, but how can I generate an xml
I know this sort of question has been asked before , but I still
This is a beginner's question but for some reason I cannot find the answer
I have this homework. And I am beginner C# program, and now learn the
this is probably a simple question, but I'm only a beginner so... Suppose I

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.