Help with Parsing A File

Write your question here.
I have been doing this project for class awhile now but I cant seem to get it to fully work.The basic gist of the problem is to write code that parses through a file locating zipcode in the form zip:XXXXX.Everytime I run it I end up undercounting and the following result:

Expected Output:

11210·4↵
44074·3↵
11230·2↵
10003·1↵
Actual Output:

44074·3↵
11210·2↵
11230·2↵
10003·1↵

I believe the issue lies in my zipFreq() but I cant make sense of what im doing wrong for it to keep undercounting the zips from the input.Any help would be appreciated.
[code]
#include <iostream>
>
> using namespace std;
>
> const int SIZE = 1000;
>
> int zipRead(string[], int);
> int extractZips(string[], int, string[], int);
> int arrayWithoutDup(string[], int, string[], int);
> bool checkIfZipAlreadyThere(string,string [], int );
>
> bool validate(string);
>
> struct Zips{
> string s;
> int freq;
> };
>
> void populateStruct(Zips[], string[], int);
> void findFreq(Zips[],int,string [],int);
> int countDuplicates(string, string,int);
> void sortZips(Zips[], int);
> void PrintTopTen(Zips [],int );
>
>
> int main(){
> string raw[SIZE];
> int raw_size=0;
>
>
> raw_size=zipRead(raw, SIZE);
>
> string w_dup[SIZE];
> int w_dup_size=0;
> w_dup_size = extractZips(raw, raw_size, w_dup, w_dup_size);
>
> string no_dup[SIZE];
> int no_dup_size=0;
> no_dup_size=arrayWithoutDup(w_dup, w_dup_size, no_dup, no_dup_size);
> //printArray(w_dup, w_dup_size);
>
> //printArray(no_dup, no_dup_size);
> Zips myZips[no_dup_size];
> populateStruct(myZips, no_dup, no_dup_size);
>
> findFreq(myZips,no_dup_size,w_dup,w_dup_size);
> sortZips(myZips,no_dup_size);
> PrintTopTen(myZips,no_dup_size);
> //printArrayZ(myZips,no_dup_size);
> return 0;
>
>
>
>
> }
> //Read in zips
>
> int zipRead(string arrZ[], int z){
> int count=0;
> for(int i = 0; !cin.fail() && i < z; i++){
> getline(cin,arrZ[i]);// reads string into array position
>
> count++;
> }
>
> return count;
> }
>
> //extraction
>
> int extractZips(string r[], int rs, string w[], int ws){
> string temp;
> for (int i=0; i<rs; i++){
> for (size_t pos=0; pos!=string::npos; ){
>
> pos = r[i].find("zip: ", pos);
> if (pos!=string::npos){
> temp = r[i].substr(pos+5,5);
> if(validate(temp)){
> w[ws]=temp;
> ws++;
> }
> pos+=5;
> }
>
> }
> }
> return ws;
> }
>
> bool validate(string s){
> for (int i =0; i<5; i++){
> if ( isdigit(s[i]) == 0 ){
> return false;
> }
> }
> return true;
> }
>
> int arrayWithoutDup(string wd[], int wds, string nd[], int nds){
> for (int i=0; i<wds; i++){
> if(!checkIfZipAlreadyThere(wd[i],nd,nds)){
> nd[nds]=wd[i];
> nds++;
> }
> }
> return nds;
> }
>
> bool checkIfZipAlreadyThere(string s,string a[], int as){
> for (int i=0; i<as; i++){
> if(s==a[i]){
> return true;
> }
> }
> return false;
> }
>
> void populateStruct(Zips z[], string a[], int s){
> for(int i=0; i<s; i++){
> z[i].s=a[i];
> }
> }
>
> void findFreq(Zips z[],int zs,string a[],int as){
> int count=0;
> for(int i=0; i < zs; i++){
> for(int j = 0; j < as; j++){
> if (z[i].s == a[j]){
> count++;
> }
> }
> z[i].freq=count;
> count=0;
>
>
> }
>
> }
>
> //sort zips
> void sortZips(Zips z[],int zs){
> for (int k = 0; k < zs; k++){
> for (int i = 0; i <zs -1 - k; i++)
> if (z[i].freq > z[i+1].freq){
> Zips temp = z[i];
> z[i] = z[i + 1];
> z[i + 1] = temp;
>
> }
> }
> for(int k = 0; k < zs; k++){ //tie in frequency
> if (z[k].freq == z[k+1].freq){
> if (z[k+1].s > z[k].s) {// swap values
> Zips temp = z[k];
> z[k] = z[k+1];
> z[k+1]=temp;
> }
> else {
> if (z[k].s < z[k+1].s)
>
> //cout << z[k].s << z[k + 1].s << endl;
> z[k] = z[k];
> z[k+1] = z[k+1];
> }
>
> }
>
> }
>
>
>
> }
>
>
> void PrintTopTen(Zips a[],int p){
> for (int i = (p - 1); i > p-1-10; i--)
> if (i>=0){
> cout << a[i].s << " " << a[i].freq <<"\n";
> }
> }
Topic archived. No new replies allowed.