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.
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: