Writing in an area code from a number in a sequential file.

I am writing a code that reads from a sequential file. It recieves the name, state and phone number. I need to add in the area code based on the state linked to that name and phone number in the .txt file I also need to attach a zipcode based on the area code, does that make sense?

SC: area code 864 and zip 29301 and Cost 100.51
CA: area code 949 and zip 95203 and Cost 99.50
KY: area code 502 and zip 50239 and Cost 89.55


Heres the code:

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
68
69
70
71
72
73
    #include <iostream>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
	system("color f0");
	ifstream inputfile;
	cout << "\t\t************************************" << endl;
	cout << "\t\t*                     *" << endl;
	cout << "\t\t*             *" << endl;
	cout << "\t\t*                 *" << endl;
	cout << "\t\t************************************" << endl;

	//Declare variables
	string Fname = " ";
	string Lname = " ";
	string Phone = " ";
	string State = " ";
	string Zipcode = " ";
	int count = 0;
	int Cost = 0;



	// Open File
	inputfile.open("customer.txt");
	inputfile >> Fname >> Lname >> Phone >> State;

	//Header
	cout << "Name \t\tState \t\tZip Code \tPhone Number \tCost"  <<endl;
	cout << "________ \t____ \t\t____\t_______ \t ________" << endl;

	//Work
	while (inputfile.eof() == false)
	{
		count++;

	

		
		
		
		
		
		
		
		Phone.insert(0, "(");
		Phone.insert(4, ")");
		Phone.insert(8, "-");

		



		cout << " " << Lname << ". " << Fname.substr(0, 1) << "\t" << " " << State << "\t" << " " << Zipcode
			<< "\t" << " " << Phone << "\t" << " " << Cost << endl;
		inputfile >> Fname >> Lname >> Phone >> State;
	}







	cout << "Number of Records " << count << endl;
	cout << "\n T H A N K   Y O U\n" << endl;
	system("pause");
	return 0;


And the .txt file is relatively short but here it is.
First Last 5924896 SC
Kim Basinger 3913003 CA
Jodie Foster 5915005 CA
Russell Crowe 6916006 KY
Britney Spears 7917007 SC
Brad Pitt 8918008 SC
Alicia Keys 9919009 CA
Charlize Theron 1911001 SC
Bonnie Raitt 2912002 KY
Taylor Swift 3913005 SC

Last edited on
Do you realize that most states have many more than one area code within their boundaries?

Some counties have more than one area code?

Some zip codes also have multiple area codes?

So how are you going to assign a single area code based on the limited data you have?




Hey, jlb, the OP’s question isn’t perfectly-formed English, but it does show both input file formats — one of which is a state/area code/zip table.

If this looked like a work work kind of problem, I would recommend the OP to Google’s libphonenumber.


@jakerg777
You need to create either a struct or three individual arrays and load your lookup table into memory so that it can be searched.

1
2
3
4
5
6
7
8
9
int MAX_LOOKUPS = 1000; // or 50? IDK

struct
{
  string state, area_code, zip;
}
lookup[MAX_LOOKUPS];

int count = 0;
1
2
3
4
5
6
7
int MAX_LOOKUPS = 1000; // or 50? IDK

string states[MAX_LOOKUPS];
string area_codes[MAX_LOOKUPS];
string zips[MAX_LOOKUPS];

int count = 0;

I put a whole lot of look-ups, I don’t know how many you actually need. Take a look at your file.

Load that file into the structure/arrays, keeping track of how many items you have in count.

THEN work on your input file. For each line you read, separate it into names, phone, and state. Find the state in your lookup array(s), then print all the new information.



Good luck!
Topic archived. No new replies allowed.