Ok I think I've gotten a step closer, as It now counts the occurrences of the first digit and calculates the frequency successfully.
What I'm still not sure of, is why my code for filtering out the comments is working sometimes, but not always. It seems to be filtering out every comment except for the one I mentioned above with the type of data format I've posted.
If I try another file with different comments such as the following, it is filtering out the first, third, and fifth line, but not the second and fourth ones.:
(* LiveJournal data collected by Shirley Man from *)
(*
http://www.livejournal.com/stats/stats.txt *)
(* Number of new accounts on LiveJournal, *)
(* day by day from 2000/1/1 to 2005/2/28 *)
(* Individual data are NOT labelled. *)
10
4
12
5
7
5
15
23
56
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 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main() {
cout << "Enter the name of the file to use, please use the specific path name with a forward slash instead of a back slash:" << endl;
string text_file;
getline (cin, text_file);
vector<int> count(10);
double count_total = 0.0;
ifstream infile{text_file.c_str()};
string line;
while(infile >> line){
if(line.at(0) =='(' && line.at(1) == '*'){
while (infile >> line) {
if(line.at(0) == '*' && line.at(1) == ')' ){
infile >> line;
break;
}
}
}
if (isdigit(line[0])){
count.at(line.at(0) - '0')++;
}
cout << line << endl;
}
infile.close();
for(unsigned int i = 1; i <= 9; i++){
count_total = count_total + count.at(i);
}
cout << "Digit Count Frequency" << endl;
for(unsigned int i = 1; i <= 9; i++ ){
cout << " " << i << " " << count.at(i) << " " << fixed << setprecision(2) << count.at(i)/count_total << endl;
}
//copy(begin(count), end(count), ostream_iterator<int>(cout, " "));
return 0;
}
|