2

The program I am creating will read in data from a text file, which contains a whole bunch of addresses and zip codes.

My question is: every time the file reads in "zip:" (if(text == "zip:"), the program should print out the tokens that come after it (the specifications ask for token oriented input), meaning the zip code numbers.

Is there a function of some kind that will only print out the zip code and none of the other text that comes after it? Sorry for the long post just want to give as much detail to the program as possible. If there is any other information I should include please let me know. I'm not looking for someone to give me complete program, just some guidance on that specific problem would be much appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main() {
    string text;
    ifstream inFile;
    inFile.open("zips");
    while(!inFile.fail()) {
        inFile >> text;
        if(text == "zip:") {

        }  
    }   
    inFile.close();
    return 0;
}  

The input is the file itself being looped through, the user does not enter any input. My desired output is the top ten most frequent zip codes. ex:

Zipz:   Frequency:
11204      39
11234      33
22098      27....etc.

Here is a sample of what some of the file contains.

<8975.37428190@62997216886.XmT.srvr@n325.xnot.com> cc: visa addr: 488 Cicada Avenue =4=Z city: Edmonton zip: T5T4M4 $20.00 <833.337428190@2997439800.XmT.srvr@n324.xnot.com> cc: visa addr: 48030 Nevada Blvd =4=Z city: Montecito zip: 95041 $15.00 <8354.37428190@63001226169.XmT.srvr@n326.xnot.com> cc: visa addr: 493 Park Meadow Drive =4=Z city: Alamo zip: 94521 $10.00 <8857.37428190@63001517062.XmT.srvr@n326.xnot.com> cc: mastercard addr: 893 Moraga Avenue =4=Z city: San Bruno zip: 94012 $15.00

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
illb3991
  • 21
  • 2

2 Answers2

1

Assuming your input file will always be formatted like you posted, which means there will always have a value for zip(I didn't check for corner cases), that should do it:

#include <iostream>
#include <string>
#include <fstream>
#include <map>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("test");

    string text;

    map<string, int> frequencies;

    while (!inFile.fail())
    {
        inFile >> text;

        if (text == "zip:" && !inFile.fail())
        {
            string zip;
            inFile >> zip;

            if (frequencies.find(zip) == frequencies.end())
                frequencies[zip] = 1;
            else
                frequencies[zip]++;
        }
    }

    map<string, int>::iterator it = frequencies.begin();
    while (it != frequencies.end())
    {
        cout << (*it).first << ": " << (*it).second << endl;
        ++it;
    }

    return 0;
}

Ran it on your example file with 1 duplicate and got this output:

94012: 1
94521: 1
95041: 2
T5T4M4: 1

Formatting and sorting is missing though. Sorting can be implemented by putting the values from the map in a container supporting sort like set or vector for example.

Have a look at these answers to see how it can be done:

Vector: https://stackoverflow.com/a/8640935/109960
Set: https://stackoverflow.com/a/2699101/109960

Community
  • 1
  • 1
Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
0

Well, based on the file you have provided above I would go about solving this problem in this manner. I'm not going to provide field-tested, actual C++ code, but a general procedure.

First I would create a data structure to aggregate all the information we can get.

// Store all the zip codes
std::vector<int> codes;

Then I would begin to read the file character by character.

  std::ifstream is(str);     // open file

  while (is.good())          // loop while extraction from file is possible
  {
    char c = is.get();       // get character from file
    if (is.good())
    {
      if(c == 'z')
        if(is.get() == 'i')
          if(is.get() == 'p')
            if(is.get() == ':')
            {
              // Extract the next 6 characters from the stream
              // and store them as a string or something
              // which you can later convert into an integer
              // and push into the data structure we created earlier
            }
    }

  }

  is.close();                // close file

You can later count up the occurrences of each zip code in the vector, and then store up information about such in a std::map.

turnt
  • 3,235
  • 5
  • 23
  • 39