Area Codes Help!

Pages: 12
Okay I know how to open the file and what not but my professor has put a bit of a twist on it and I'm not particularly sure how to do it this time....Please help!!
The code underneath is what I have so far, not sure if I should be using stuff, some stuff I took here and there from the internet to try and figure out...

Here is what my professor wants:
Telephone numbers in the US and Canada are organized into various three-digit area codes. A
single area code will not cross a state or provincial boundary. This rule makes it possible to list the
geographical locations of each area code in a data file. Write a C++ program that reads the file into a STL
Map<int, string> where the key is the area code and the value is the location. Let the user enter the
name of the state or providence from the keyboard and have the program list all the area codes that serve
that area. Text file will be provided by the instructor. Let the user enter the file name from the keyboard.
Output should look similar to below.
Sample Run:
Enter the file name: areacodes.txt
Enter the state name: Oregon
458
503
541
971


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
  #include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>

using namespace std;



int main()
{

	map<int, string> States;


	ifstream inputFile;
	string fileName;
	string StateName;



	cout << "Enter the file name: ";
	cin >> fileName;


	inputFile.open(fileName);


	cout << "Enter the state name: ";
	cin >> StateName;


	if (StateName.begin() != StateName.end())
	{

		getline(inputFile, StateName);


		while (StateName.find(StateName, 0))
		{
			istringstream lineStream(StateName);
			lineStream >> StateName;
			cout << StateName;

			getline(inputFile, StateName);

			inputFile.close();
		}
	}
	else
	{


		cout << "Error: Cannot open file!" << endl;

	}

	cin.ignore();
	cin.get();
	return 0;


	
}


Here is the txt file if needed:

201-New Jersey
202-District of Columbia
203-Connecticut
204-Manitoba
205-Alabama
206-Washington
207-Maine
208-Idaho
209-California
210-Texas
212-New York
229-Georgia
234-Ohio
236-British Columbia
239-Florida
240-Maryland
248-Michigan
249-Ontario
250-British Columbia
251-Alabama
252-North Carolina
253-Washington
254-Texas
256-Alabama
260-Indiana
262-Wisconsin
267-Pennsylvania
269-Michigan
270-Kentucky
274-Wisconsin
276-Virginia
278-Michigan
281-Texas
283-Ohio
289-Ontario
301-Maryland
302-Delaware
303-Colorado
304-West Virginia
305-Florida
306-Saskatchewan
307-Wyoming
308-Nebraska
309-Illinois
310-California
312-Illinois
313-Michigan
314-Missouri
315-New York
316-Kansas
317-Indiana
318-Louisiana
319-Iowa
320-Minnesota
321-Florida
323-California
325-Texas
330-Ohio
331-Illinois
334-Alabama
336-North Carolina
337-Louisiana
339-Massachusetts
341-California
343-Ontario
347-New York
351-Massachusetts
352-Florida
360-Washington
361-Texas
364-Kentucky
369-California
380-Ohio
385-Utah
386-Florida
401-Rhode Island
402-Nebraska
403-Alberta
404-Georgia
405-Oklahoma
406-Montana
407-Florida
408-California
409-Texas
410-Maryland
412-Pennsylvania
413-Massachusetts
414-Wisconsin
415-California
416-Ontario
417-Missouri
418-Quebec
419-Ohio
423-Tennessee
424-California
425-Washington
430-Texas
432-Texas
434-Virginia
435-Utah
438-Quebec
440-Ohio
442-California
443-Maryland
445-Pennsylvania
447-Illinois
450-Quebec
458-Oregon
464-Illinois
469-Texas
470-Georgia
475-Connecticut
478-Georgia
479-Arkansas
480-Arizona
484-Pennsylvania
501-Arkansas
502-Kentucky
503-Oregon
504-Louisiana
505-New Mexico
506-New Brunswick
507-Minnesota
508-Massachusetts
509-Washington
510-California
512-Texas
513-Ohio
514-Quebec
515-Iowa
516-New York
517-Michigan
518-New York
519-Ontario
520-Arizona
530-California
531-Nebraska
534-Wisconsin
540-Virginia
541-Oregon
551-New Jersey
557-Missouri
559-California
561-Florida
562-California
563-Iowa
564-Washington
567-Ohio
570-Pennsylvania
571-Virginia
573-Missouri
574-Indiana
575-New Mexico
579-Quebec
580-Oklahoma
581-Quebec
585-New York
586-Michigan
587-Alberta
601-Mississippi
602-Arizona
603-New Hampshire
604-British Columbia
605-South Dakota
606-Kentucky
607-New York
608-Wisconsin
609-New Jersey
610-Pennsylvania
612-Minnesota
613-Ontario
614-Ohio
615-Tennessee
616-Michigan
617-Massachusetts
618-Illinois
619-California
620-Kansas
623-Arizona
626-California
627-California
628-California
630-Illinois
631-New York
636-Missouri
641-Iowa
646-New York
647-Ontario
650-California
651-Minnesota
657-California
659-Alabama
660-Missouri
661-California
662-Mississippi
667-Maryland
669-California
678-Georgia
679-Michigan
681-West Virginia
682-Texas
689-Florida
701-North Carolina
702-Nevada
703-Virginia
704-North Carolina
705-Ontario
706-Georgia
707-California
708-Illinois
709-Newfoundland & Labrador
712-Iowa
713-Texas
714-California
715-Wisconsin
716-New York
717-Pennsylvania
718-New York
719-Colorado
720-Colorado
724-Pennsylvania
727-Florida
730-Illinois
731-Tennessee
732-New Jersey
734-Michigan
737-Texas
740-Ohio
747-California
752-California
754-Florida
757-Virginia
760-California
762-Georgia
763-Minnesota
764-California
765-Indiana
769-Mississippi
770-Georgia
772-Florida
773-Illinois
774-Massachusetts
775-Nevada
778-British Columbia
779-Illinois
780-Alberta
781-Massachusetts
785-Kansas
786-Florida
787-Puerto Rico
801-Utah
802-Vermont
803-South Carolina
804-Virginia
805-California
806-Texas
807-Ontario
808-Hawaii
810-Michigan
812-Indiana
813-Florida
814-Pennsylvania
815-Illinois
816-Missouri
817-Texas
818-California
819-Quebec
828-North Carolina
830-Texas
831-California
832-Texas
835-Pennsylvania
843-South Carolina
845-New York
847-Illinois
848-New Jersey
850-Florida
856-New Jersey
857-Massachusetts
858-California
859-Kentucky
860-Connecticut
862-New Jersey
863-Florida
864-South Carolina
865-Tennessee
867-Northwest Territories
870-Arkansas
872-Illinois
878-Pennsylvania
902-Nova Scotia
903-Texas
904-Florida
905-Ontario
906-Michigan
907-Alaska
908-Alaska
909-California
910-North Carolina
912-Georgia
913-Kansas
914-New York
915-Texas
916-California
917-New York
918-Oklahoma
919-North Carolina
920-Wisconsin
925-California
927-Florida
928-Arizona
931-Tennessee
935-California
936-Texas
937-Ohio
938-Alabama
939-Puerto Rico
940-Texas
941-Florida
947-Michigan
949-California
951-California
952-Minnesota
954-Florida
956-Texas
957-New Mexico
959-Connecticut
970-Colorado
971-Oregon
972-Texas
973-New Jersey
975-Missouri
978-Massachusetts
979-Texas
980-North Carolina
984-North Carolina
985-Louisiana
989-Michigan

Last edited on
You need to read the whole file, and "insert" the area code and name into your map before you try to search your "database" for the information.

Hello Unisaurus,

You have a good start to your program. I will suggest that at line 28 you should check to make sure the file is open. Use this code if you like:
1
2
3
4
5
6
7
8
9
10
11
if (inFile.is_open())
{
	std::cout << "\n File " << fileName << " is open" << std::endl;
	//Sleep(SLEEP_TIME);
}
else
{
	std::cout << "\n File " << fileName << " did not open" << std::endl;
	Sleep(SLEEP_TIME);
	exit(1);
}

I use the Sleep() function until I find something different. It has not been important yet.

Line 34 I do not believe that the condition will work. I am thinking it will always evaluate to true. Also StatName is a string not a map, so I do not think the ,begin() and .end() will not work the way you are thinking.

Line 40 the while statement is mostly wrong. Line 48 should be just above line 59. And line 46 is the wrong place to rad the file. It should be earlier when you load the map.

After you open the file then you need to load the map. These links should help:
http://www.cplusplus.com/reference/map/map/
http://www.cplusplus.com/reference/map/map/map/

Hope that helps,

Andy
Thanks so much for the help Andy!! Yeah I'm tryin to learn what and how to use the map lol. I'll post more of my code by the end of the weekend.
Okie dokie here is some updated 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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
#include <windows.h>


using namespace std;



int main()
{

	map<int, string> States;


	ofstream inputFile;
	string fileName;
	string StateName;
	string num;
	

	cout << "Enter the file name: ";
	cin >> fileName;

	
	inputFile.open(fileName.c_str());


	cout << "Enter the State name: ";
	cin >> StateName;


	if (inputFile)
	{
		while (!inputFile.eof()) // a check for we are not at the end of file.
		{

			
			 cout << num.substr(0, 3);
			inputFile << StateName; // we are reading from a file....
			inputFile << num;
			
			cout << "Word Number " <<  ": " << StateName << num << endl;

		}

		inputFile.close();
	}
	else
	{
		cout << "We had a problem with the file!!!" << endl;
	}


	

	inputFile.close();



	cin.ignore();
	cin.get();
	return 0;


	
}


Please any help is appreciated.
Okie dokie I can read and print all of the area code numbers so now I just need it specifically find the certain states numbers.

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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
#include <windows.h>


using namespace std;



int main()
{

	map<int, string> States;


	ifstream inputFile;
	string fileName;
	string StateName;
	int num;
	

	cout << "Enter the file name: ";
	cin >> fileName;

	
	inputFile.open(fileName);


	cout << "Enter the State name: ";
	cin >> StateName;


	if (inputFile)
	{
		while (!inputFile.eof()) // a check for we are not at the end of file.
		{
			
			while (getline (inputFile, StateName))
			{

				 
					inputFile >> num;
					cout << num << endl;
				
			}
		
		}

		inputFile.close();
	}
	else
	{
		cout << "We had a problem with the file!!!" << endl;
	}


	

	inputFile.close();



	cin.ignore();
	cin.get();
	return 0;


	
}
Why do you have that map<>?

Have you considered using it somehow?



Hello Unisaurus,

As jlb said you have defined a map called “States”, but you have never used it as the instructions call for.

Your outer while loop at line 38 is not needed and checking for “eof” is bad form and may not work as you might expect. You will reach “eof” before you can check for it.

The inner while loop at line 41 is where you will need to load the map. I read a single line from the file with
while (getline(inputFile, line))
Then I used a stringstream to break up the line into the area code and state name then change the area code which is a string to a number for variable “num” before storing the two pieces of info into the map.
Following the while loop I created an iterator to use in a for loop to printing out the area codes for a state. Followed by an input for which state to find. Then a for loop to loop through the map to match the state and print the area code(s) for that state.

Today I had the thought to add code if no match was found.

The two links from my earlier post http://www.cplusplus.com/forum/beginner/202015/#msg962523 will help to understand using “map”.

Hope that helps,

Andy
Yeah I keep forgetting about the map because my professor just requested us to use it and I have no idea about it I just know what it stands for... but thank you guys so much for the help! I'll try and post what I have updated later tonight.
Okay this is as far as i've gotten today, i'll try some more tomorrow and its due on sat! Trying my best to learn and write good code. Any help is again appreciated.

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
74
75
76
77
78
79
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
#include <windows.h>


using namespace std;



int main()
{

	map<int, string> States;


	ifstream inputFile;
	string fileName;
	string StateName;
	string line;
	string num;
	

	cout << "Enter the file name: ";
	cin >> fileName;

	
	inputFile.open(fileName);


	cout << "Enter the State name: ";
	cin >> StateName;


	if (inputFile)
	{
		
			while (getline (inputFile, line))
			{
				
				stringstream iss(line); // I used this and the last two and it printed out the area codes and the states...not sure if I'm going in the right direction or not.
				int num = atoi(num);  //Not sure if I need this and for some reason it will NOT turn to a int i've tried everything I know and googled some stuff...
				States.insert(pair <int, string> (num, StateName)); // Or this
				getline(iss, num);
				cout << num << endl;
				
			}
		
			for (int a = 0; a < num; a++)
			{




			}

		inputFile.close();
	}
	else
	{
		cout << "We had a problem with the file!!!" << endl;
	}


	

	inputFile.close();



	cin.ignore();
	cin.get();
	return 0;


	
}
Okay using a stringstream to process isn't a bad idea, but you don't need atoi(), that's where the stringstream comes into play:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    while(getline(inputFile, line))
    {
        // Use a stringstream help parse each line.
        stringstream iss(line);
        // Now parse each line, first strip off the area code and the dash.
        char dash;
        // Get the area_code the retrieve and discard the dash.
        iss >> area_code >> dash;
        // Now get the state/province name.
        getline(iss, state_name);

        // Now print the two variables to see that you've properly read the "file".
        cout << "Area code " << area_code << " is in " << state_name << endl;

        // Here is where you need to populate your map.
 

    }


Hint you don't really need make pair, just use array notation to add the key/value to the map.
http://www.cplusplus.com/reference/map/map/operator[]/

And by the way you really don't need to close your input file, let the class destructor do it's job, it'll automatically close the file when the stream goes out of scope.

Hello Unisaurus,

Basd on what I did and what you have it would work better this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (getline(inputFile, line))
{

	stringstream iss(line); // I used this and the last two and it printed out the area codes and the states...not sure if I'm going in the right direction or not.
	std::gtline(iss, snum, '-'); // snum is a string and this line will extract from the stringstream u to the '-', but not including the '-'.
	std::getline(iss, StateName); // This will extract the state name after the '-'.
	int num = atoi(snum);  // snum is defined as type string. Same concept as what you have, but easier to understand.
	States.insert(pair <int, string>(num, StateName)); // Or this
	//States.insert(make_pair(num, StateName));  // I believe this is the correct way to code the above line. As jlb said you do not need to add to
                                                     // the map this way I just thought it was a better way to build a map of unknown size.You can set
                                                     // this up any way you like.

	cout << num << endl;
}

Based on your last bit of code line 46 is not needed. The condition of the while loop will do the job. Line 44 does not work because you have taken nothing fro the stringstream and put it in "num". Also using the same name (num) for two different types is a bad idea. Even if it compiles it is hard to know which one is which.

Or based on jlb's suggestion you might try this. A bit cleaner and I see the truth in it now.
1
2
3
4
5
6
7
8
9
while (getline(inputFile, line))
{

	stringstream iss(line); // I used this and the last two and it printed out the area codes and the states...not sure if I'm going in the right direction or not.
	iss >> num >> dash;
	getline(iss,  StateName); // Corrected
	States.insert(make_pair (num, StateName)); // Or this
	cout << num << endl;
}


Hope that helps,

Andy

Edited to correct earlier mistake.
Last edited on
iss >> StateName;

This won't work, several of the StateNames have spaces, the insertion operator>> stop processing strings when it encounters a whitespace character.

Your call to atoi() will not be needed if you use the proper type of variable in the first place (an int). In my snippet area_code is declared as an int, not a string. By the way atoi() really shouldn't be used in a C program much less a C++ program, this C function can silently fail, plus this function expects a C-string not a C++ std::string.

And as I stated in my last post you can use "array" notation to insert the key/values into the map, instead of the make_pair() call.

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
    ...
    std::map<int, std::string> areaCodes;

    string line, StateName;
    int area_code; 
    while(getline(inputFile, line))
    {
        // Use a stringstream help parse each line.
        stringstream iss(line);
        // Now parse each line, first strip off the area code and the dash.
        char dash;
        // Get the area_code the retrieve and discard the dash.
        iss >> area_code >> dash;
        // Now get the state/province name.
        getline(iss, state_name);

        // Now print the two variables to see that you've properly read the "file". Can be removed after degugging.
        cout << "Area code " << area_code << " is in " << state_name << endl;

        // Here is where you need to populate your map.
        areaCodes[area_code] = state_name;
    }

    // Print out the map contents.
    for(auto& mm : areaCodes)
        cout << "Area code " << mm.first << " is in " << mm.second << endl;


@jlb,

Thanks I missed that when I was typing. Think to far ahead.

Andy
Okie dokie I'm starting to get it now. However the only thing I have to do now is only print out the certain area codes for the specific state that you type in. Other than that it prints everything out nicely!
However the only thing I have to do now is only print out the certain area codes for the specific state that you type in.

If you need to look up the states to print each area code for that state then you probably shouldn't be using a map. A map is designed for quick lookup based on the key, which in this case is the area code, and the key must be unique. I would recommend either a std::multimap<string, int> if you need to print each area code for a state. Another option would be to use a class/structure that holds the area code and state name, then create a vector of this class to hold the information. You can then sort the vector for the proper "lookup" order.


Okie dokie thanks sadly I have to use a map my professor said that I have to use it...specifically this :

Write a C++ program that reads the file into a STL
Map<int, string> where the key is the area code and the value is the location.

Gah Im super confused this professor is horrid!

** On the map should I put the string then the int first? So that way it can look for the state than look for the area codes?
Last edited on
On the map should I put the string then the int first? So that way it can look for the state than look for the area codes?

No, you have duplicate state names and the key must be unique.
So I think maybe I might be on to something but I have until midnight of today (19th) to have this done... lol so kinda in panic mode.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
#include <windows.h>


using namespace std;



int main()
{

	map<int, string> States;
	

	ifstream inputFile;
	string fileName;
	string StateName;
	string line;
	int num;
	

	cout << "Enter the file name: ";
	cin >> fileName;

	
	inputFile.open(fileName);


	cout << "Enter the State name: ";
	cin >> StateName;


	if (inputFile)
	{
		
		while (getline(inputFile, line))
		{
			char dash;
				// Use a stringstream help parse each line.
				stringstream iss(line);

				iss >> num >> dash; // I figured I would put this hear to strip off the area code so that it could find the  state...

				if (line == StateName)
				{
					cout << num << endl;
					// Now parse each line, first strip off the area code and the dash.
					char dash;
					// Get the area_code the retrieve and discard the dash.
					iss >> num >> dash;
					// Now get the state/province name.
					getline(iss, StateName);
				}
			
				
			// Here is where you need to populate your map.
			States[num] = StateName;
		}

		// Print out the map contents.
		for (auto& mm : States)
			cout << "Area code " << mm.first << " is in " << mm.second << endl;

			
	}
	else
	{
		cout << "We had a problem with the file!!!" << endl;
	}


	



	cin.ignore();
	cin.get();
	return 0;


	
}
Hello Unisaurus,

Almost there. Line 56 should be at line 47 and lines 54 - 56 are not needed here because the while loop will take care of this. As it is you will load the "map" with every second line and miss half the file.

At line 65 you need to ask the user for the state to find. After that I printed out the state name before the for loop. in the for loop I compared "mm.second" to the state name to find and printed out the area code if the matched.

Hope that helps,

Andy
Pages: 12