-1

I am entirely new to programming so I'm sorry if I don't explain this well. For my C++ assignment I had to write an object-oriented program that reads the names from a text file (the text file is just a list of first names) and prints them to the console in alphabetical order using an array. Originally, the description of the assignment said that the file had 20 names, so I based my code around that. The program works, but now it turns out the assignment description was inaccurate and we shouldn't assume that the text file has a specific number of names. How do I convert my code from specifically reading 20 names to instead reading an undefined number of names, while still using an array? I don't fully understand the concepts that I'm implementing so it's difficult for me to know how to change my code while still following the requirements of the assignment. Here is my code:

#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>

using namespace std;


class Names
{
private:
    ifstream inStudents;
    string studentNames[20];
    string name; 
    int j; 

public:
    Names();
    ~Names();
    void openFile(string);
    void testFile();
    void readFile();
    void sortNames();
    void closeFile();
    void display();
};

Names::Names()
{

}
Names::~Names()
{

}
void Names::openFile(string d) 
{
    inStudents.open(d); 

}

void Names::testFile()
{
    if (!inStudents)
    {
        cout << "File did not open" << endl;
        exit(10);
    }
}

void Names::readFile()
{
    cout << "Reading the input file..." << endl; 
    int j = 0; 
    while (inStudents >> name && j < 20)
    {
        studentNames[j++] = name;
    }
}

void Names::sortNames() 
{ 
    sort(studentNames, studentNames + 20);
}

void Names::closeFile()
{
    inStudents.close();
}

void Names::display() 
{ 
    cout << endl << "The alphabetical list: " << endl << endl;
    for (int i = 0; i<20; i++)
        cout << studentNames[i] << " " << endl;
    cout << endl << endl;
}

int main() 
{
    Names r;
    r.openFile("students.txt");
    r.readFile();
    r.testFile();
    r.sortNames();
    r.display();
    r.closeFile();

    return 0;
}
sfu
  • 13
  • 1
  • 2
    If the assignment allows it, replace `string studentNames[20];` with `vector studentNames;` and [read up on the `push_back` method](https://en.cppreference.com/w/cpp/container/vector). A great may assignments like this do not allow use of `std::vector` because they want you to figure out how to write your own. If this is the case for you search stack overflow for questions from folk who have had to write their own `vector` for suggestions on how to do this correctly. Recommended reading: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – user4581301 Aug 11 '18 at 22:06
  • Arrays in C++ must have their length declared when defined. So your choice is to have a very large array size, possibly in global space (not declared inside functions) and keep track of the number of entries read with a hard stop if the limit is exceeded or, as suggested, use `std::vector` which is designed for things like this and also easily sorted. But the assignment sounds like it's directing you to use an array. – doug Aug 11 '18 at 23:13

1 Answers1

0

You can use std::vector object instead of a regular array. It will look like that:

vector<string> studentNames;

Now, instead of using the following line to insert a name to a known place in the array:

studentNames[j++] = name;

use:

studentNames.push_back(name);
//or
studentNames.emplace_back(name);

The the while loop inside your readFile function, will look like this:

while (inStudents >> name)
{
    studentNames.push_back(name);
}

To display it now, all you have to change in your display function is the range. The vector object include a function named size which returns you the current vector size, or in other words- the elements' count that the vector includes. It will seem like the following line:

for (int i = 0; i < studentNames.size(); i++)
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22