Area Codes Help!

Pages: 12
Thank you sooo much Andy and jib y'all helped me soo much and I actually learned what to do sorta still gonna practice! Here is the final code that works!! :D

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
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;
	char answer;

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


		inputFile.open(fileName);


		if (inputFile)
		{

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

				iss >> num >> dash;
				getline(iss, StateName);


				// Here is where you need to populate your map.
				States[num] = StateName;
			}

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


			// Print out the map contents.
			for (auto& mm : States)

				if (mm.second == StateName)
					cout << mm.first << endl;
		}
		else
		{
			cout << "We had a problem with the file!!!" << endl;
		}

		


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


	
}
Last edited on
Hello Unisaurus,

Good Job. Now for some fun when you have a chance wrap most of the program in a do while loop to process more than one state.

Andy
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <map>
#include <string>

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;
    cin.ignore(1000, '\n');
    
    inputFile.open(fileName);
    
    if (inputFile)
    {
        while (getline(inputFile, line))
        {
            num = stoi(line.substr(0,3));
            StateName = line.substr(4);
            
            States.insert( pair<int,string>(num,StateName) );
        }
    }
    else
    {
        cout << "We had a problem with the file!!!" << endl;
    }
    
    cout << "Enter the State name: ";
    getline(cin, StateName);
    
    for (auto it = States.begin(); it!= States.end(); ++it)
    {
        if (it -> second == StateName)
            cout << it -> first << ' ';
    }
    
    std::cout << '\n';
    
    return 0;
}


This is just another way of doing it. Persevere with what you have and that way be proud to call it your own. Preferably don't just copy and (oops I meant don't ignore) ignore all the advice you have already received.

PS All this comes from directly using the tutorials and reference material on this site using the sample programs. You don't need to be a walking C++ encyclopedia ;)
Last edited on
Andy I was going to do that! But I did not want to mess up the code because it actually worked, and than after I turned it in someone messaged me telling me that states like "New Mexico" doesnt work oops lol

Kemont I will definetly need to take some time and sit down and look at the differences between the codes lol

I really appreciate all y'alls help it seriously saved me! :)
Topic archived. No new replies allowed.
Pages: 12