Conversion thingy

Instead of actually showing the zip code from the .txt file I need it to go to another txt file and match it with the city it corresponds with, how would I go about doing that? Below is my code so far without converting the zip codes to the cities stated in the .txt file...

Please keep in mind in the .txt file theres 200 lines of information and they are in this format..
48001 42.61500 -82.59780 MI Algonac

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
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;


void main ()
{
{
string date1, zip1, direction1, Rate1, Customer1, date2, zip2, direction2, Rate2, Customer2, date3, zip3, direction3, Rate3, Customer3, date4, zip4, direction4, Rate4, Customer4, date5, zip5, direction5, Rate5, Customer5, date6, zip6, direction6, Rate6, Customer6, date7, zip7, direction7, Rate7, Customer7;

ifstream infile;
infile.open ("shipments.txt");
infile >> date1 >> zip1 >> direction1 >> Rate1 >> Customer1 >> date2 >> zip2 >> direction2 >> Rate2 >> Customer2 >> date3 >> zip3 >> direction3 >> Rate3 >> Customer3 >> date4 >> zip4 >> direction4 >> Rate4 >> Customer4 >> date5 >> zip5 >> direction5 >> Rate5 >> Customer5 >> date6 >> zip6 >> direction6 >> Rate6 >> Customer6 >> date7 >> zip7 >> direction7 >> Rate7 >> Customer7;

cout << "Date: " << date1 << endl
<< "Zip: " << zip1 << endl
<< "Direction: " << direction1 << endl
<< "Rate: " << Rate1 << endl
<< "Customer: " << Customer1 << endl
<< endl
<< "Date: " << date2 << endl
<< "Zip: " << zip2 << endl
<< "Direction: " << direction2 << endl
<< "Rate: " << Rate2 << endl
<< "Customer: " << Customer2 << endl
<< endl
<< "Date: " << date3 << endl
<< "Zip: " << zip3 << endl
<< "Direction: " << direction3 << endl
<< "Rate: " << Rate3 << endl
<< "Customer: " << Customer3 << endl
<< endl
<< "Date: " << date4 << endl
<< "Zip: " << zip4 << endl
<< "Direction: " << direction4 << endl
<< "Rate: " << Rate4 << endl
<< "Customer: " << Customer4 << endl
<< endl
<< "Date: " << date5 << endl
<< "Zip: " << zip5 << endl
<< "Direction: " << direction5 << endl
<< "Rate: " << Rate5 << endl
<< "Customer: " << Customer5 << endl
<< endl
<< "Date: " << date6 << endl
<< "Zip: " << zip6 << endl
<< "Direction: " << direction6 << endl
<< "Rate: " << Rate6 << endl
<< "Customer: " << Customer6 << endl
<< endl
<< "Date: " << date7 << endl
<< "Zip: " << zip7 << endl
<< "Direction: " << direction7 << endl
<< "Rate: " << Rate7 << endl
<< "Customer: " << Customer7 << endl;

infile.close();

}
system ("pause");
}


An example would be helpful
Before main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Shipment
{
    std::string date,
                zip,
                direction,
                rate,
                customer;

    friend std::istream &operator>>(std::istream &in, Shipment &s)
    {
        return in >> s.date >> s.zip >> s.direction >> s.rate >> s.customer;
    }
    friend std::ostream &operator<<(std::ostream &out, const Shipment &s)
    {
        return out << "Date: "      << s.date      << std::endl
                   << "Zip: "       << s.zip       << std::endl
                   << "Direction: " << s.direction << std::endl
                   << "Rate: "      << s.rate      << std::endl
                   << "Customer: "  << s.customer  << std::endl;
    }
};
Then this is your main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<Shipment> shipments;

{
    std::ifstream infile ("shipments.txt");
    Shipment temp;
    while(infile >> temp)
    {
        shipments.push_back(temp);
    }
}

for(size_t i = 0; i < shipments.size(); ++i)
{
    std::cout << shipments[i] << std::endl;
}
Off the top of my head - there may be a few mistakes.

Edit: improved it a bit.
Last edited on
where exactly would i put that in the code I've done so far?
(I edited my above post)

You'd put the struct before main and the code snippet as your entire main. You also need to #include <vector>
Last edited on
This actually helped make my code look cleaned but it's not really answering my question..

For all the "Zip"s I am getting from the shipments.txt file I need to convert it so it shows as a city on the Output..I have a .txt file with every zip code from the state of michigan for example..

48001 42.61500 -82.59780 MI Algonac (theres over 200 of them in this format)

So like if "48001" showed up in my shipments.txt file it would show up as "Algonac" on my output

I'm not sure how to do that.
Last edited on
You could make another struct/vector setup for the zip/city pairs and then have a std::map where the zip is the key and the zip/city struct is the value. When you output each Shipment, you can just do e.g. MyMap[shipments[i].zip].city or similar to get the city that corresponds to the zip.
Topic archived. No new replies allowed.