Hi, I'm still getting the hang of C++ and am currently working on a project for one of my classes. My goal is to create a program that reads a series of integers from a file, places those integers into a vector, tallies those integers by creating separate vectors in pairs (the number in the file, and how many times that number is repeated in the file), then finally outputs the new paired vectors into a new file. I still haven't fully grasped how to pass vectors into functions by reference, so that's mainly where I'm struggling. I've included what I have so far. I'm not looking for anyone to completely solve this for me, I'm just hoping someone can tell me if I'm headed in the right direction, and what I should be doing/where I should be looking for the next step.
void readFile(vector<int> list, ifstream &file) here you'll need to pass your vector by reference , currently you are passing it by value, it should be something like this vector<int>& list
The syntax for pass by reference is the same regardless of the data type. Just put an '&' following the type name, as you did with ifstream. So, the desired prototype is: void readFile(vector<int> &list, ifstream &file);.
Using two other vectors to record the values tally is workable, but less than ideal. I believe the data structure given by std::map is ideal. I suppose you're following an assignment requirement though, so you must implement the solution that way.
Declare the other 2 vectors in main(), as you'll have to pass them to the displayTal function after tallyInt fills them.
You'll need to pass all 3 vectors to tallyInt. You could pass the vector n to tallyInt by value (it would work), but why force a copy of the entire vector to be made. I'd recommend passing n by constant reference because the function is intended to not modify n. This prototype would be: void tallyInt( const vector<int>& sourceVec, vector<int>& valuesVec, vector<int>& tallyVec );. tallyInt could be passed both valuesVec and tallyVec by constant reference, though it would also work without the const specifier.
Hope that gets you going.
EDIT: Sorry andy1992. I wasn't aware of your post. Yes, it took me that long to write my post!
Thank you both. Not sure why I couldn't figure out that I just needed to add &. fun2code, I'm not sure if I was misleading but I don't believe I need to use 3 different vectors. I believe I only need the original vector and then a new vector of pairs.