Pass a vector of pairs to a function, then write each pair to a file

Hi, I'm hoping for some help with some code I've been working on for a class. I need to have three functions, which are displayed below. The first one (readFile) is passed a file name and stores the ints in the file into a vector. The second (tallyInt) takes the vector and sorts it, then creates a new list of vectors that tallies the number of each int in the file. The first int in the pair is the integer found in the file, the second (unsigned) int is the number of times the first int occurs in the file. The third function is supposed to print each pair, one by one, to a new file. For whatever reason I can't figure out the third function. I think my first two functions are correct, but I'm not certain there either. Any help would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <algorithm>
#include "TallyIntegers.h"

using namespace std;

void readFile(vector<int>& list, ifstream &file){
	file.open("Prj01.ProdFile.txt", ios::in);
	int m;
	while (file >> m){
		list.push_back(m);
	}
	file.close();
	cout << "Tally: " << endl;
	for (int i = 0; i < list.size(); i++){
		cout << list[i] << endl;
	}
	cin.get();
};

void displayTal(const vector<pair<int, int>>& pairs){
	std::ofstream ofs("Prj1outfile.txt", std::ofstream::out);
	ofs << pairs << endl;
	ofs.close;
}

void tallyInt(std::vector<int>& list){
	

	std::sort(begin(list), end(list));
	std::vector<std::pair<int, int>> pairs;
	int number = 0, count = 1;
	std::pair<int, int> pair;
	for (int i = 0; i < list.size(); i++){
		number = list[i];
		while ((i+1) < list.size() && (list[i+1]==number)){
			count++;
			i++;
		}
		pair = std::make_pair(number, count);
		pairs.push_back(pair);
	}
	displayTal(pairs);
}

int main(){
	ifstream inFile;
	ofstream outfile;
	vector<int> n;
	readFile(n, inFile);
	tallyInt(n);
	//vector<pair<int, int>> k;
	//displayTal(k);
	return 0;
}
Last edited on
In displayTal() you have to display the vector one item at a time, just like you did in readFile():
1
2
3
    for (int i=0; i<pairs.size(); ++i) {
        ofs << pairs[i].first << '\t' << pairs[i].second << '\n';
    }

In tallyInt(), you need to reset the count to 1 each time through the outer loop. Otherwise the count from one number bleeds into the count for the next:
1
2
3
4
5
6
7
    for (int i = 0; i < list.size(); i++) {
        number = list[i];
        count = 1;
        while ((i + 1) < list.size() && (list[i + 1] == number)) {
            count++;
            i++;
        }


It's good to separate your computation code from the display code. Rather than calling displayTal() inside tallyInt(), call it in main:
1
2
tallyInt(n);
displayTal(n);


Do something similar with readFile(): move the code that displays the tally to its own function and call it in main after calling readFile().

Does the program have to work like this? You can read the numbers and build the tallies in one function. This code reads the numbers from cin and prints the tally to cout:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <map>
#include <utility>
#include <algorithm>

using namespace std;

void
readAndTally(map<int,int> &tally, istream &is)
{
    int m;
    tally.clear();
    while (is >> m) {
        ++tally[m];
    }
}

void
displayTal(const map<int,int> &tally)
{
    map<int,int>::const_iterator iter;
    for (iter = tally.begin(); iter != tally.end(); ++iter) {
        cout << iter->first << '\t' << iter->second << '\n';\
    }
}

int
main()
{
    map<int,int> tally;
    readAndTally(tally, cin);
    displayTal(tally);
    return 0;
}

NVM i got it now... it is because I needed to run the program from the temp folder..
Last edited on
Topic archived. No new replies allowed.