0

I've written some code where the program reads in information from a file (here's the file)

5 
Franks,Tom 2 3 8 3 6 3 5 
Gates,Bill 8 8 3 0 8 2 0 
Jordan,Michael 9 10 4 7 0 0 0 
Bush,George  5 6 5 6 5 6 5 
Heinke,Lonnie  7 3 8 7 2 5 7 

and puts it into two arrays. One for names and one for numbers. Then totals up the numbers and stores it in the data array. Now I need to change all the arrays into Vectors and I'm confused on how to do that. I know that I need to use push_back but I'm confused on how to start.

Here's the code with the arrays:

int data[50][8];
string names[50];    

int const TTL_HRS = 7;
ifstream fin;
fin.open("empdata.txt");

if (fin.fail()) {
    cout << "ERROR";
}

int sum = 0;
int numOfNames;
fin >> numOfNames;

for (int i = 0; i < numOfNames; i++) {

    fin >> names[i];

    data[i][7] = 0;

    for (int j = 0; j < 7; j++) {
        fin >> data[i][j];
        data[i][TTL_HRS] += data[i][j];
    }
}

fin.close();
return numOfNames;

}

I know that I have to make the arrays vectors. So I'll have

vector<vector<int>>data;

and

 vector<string>names;

but I'm not sure how to go about filling them.

Ralf
  • 3
  • 1
  • 5
  • Your C++ book should have at least one chapter explaining how vectors work, and how to use them. I'm sure it has an example that shows how to add or remove values from a vector. Did you read that chapter? – Sam Varshavchik Nov 12 '16 at 00:12

3 Answers3

0

You can use the .push_back() method to add the elements one by one.

By the way I'm curious, why do you want vectors instead of arrays ?

For example, for your string vector, you can change fin >> names[i]; by

string tmpString; 
fin >> tmpString; 
names.push_back(tmpString);

There is maybe (probably) a better way to do this, but that's how I see it.

FeelZoR
  • 63
  • 8
  • I'm practicing using vectors so I want to change a code I did with arrays to one with vectors – Ralf Nov 12 '16 at 00:11
  • But still, as other users said, you should read books, they all explain it correctly (probably ten thousand times better than me). – FeelZoR Nov 12 '16 at 00:23
0

It seem quite to out of topic. But I suggest you a C++11 similar structure std::array to C-array. For small and fixed size data (like your sample) it's more efficient than std::vector.

Community
  • 1
  • 1
khôi nguyễn
  • 626
  • 6
  • 15
0

I'm assuming the .txt file has this:

Franks Tom 2 3 8 3 6 3 5

Basic solution for your problem.

int main(int argc, const char * argv[]) {
        vector <int> numbers;
        vector <string> names;
        string line, name1, name2;
        int num[7];


        ifstream fin("/empdata.txt");


        if (!fin.is_open()) {
            cout << "ERROR";
        }
        else{

            while(getline(fin, line)){
                istringstream is(line);

                is >> name1;
                is >> name2;
                for(int i=0; i<7; i++){
                    is >> num[i];
                }
                names.push_back(name1);
                names.push_back(name2);

                for(int i=0; i<7 ; i++){
                    numbers.push_back(num[i]);
                }

            }
            fin.close();
        }
        //Printing what did you read from a file
        string allNames;
        for(auto it = names.begin(); it < names.end(); it++){
            allNames += (*it) + "\t";
        }
        cout << allNames << endl;

        for (auto it = numbers.begin(); it < numbers.end(); it ++){
            cout << (*it) << "\t";
        }

        return 0;
    }
jorge saraiva
  • 235
  • 4
  • 15