What I am doing wrong? Map

I have a file filled up website and their IP address
I need to hash them using map
and then ask the user for a website and output the IP address

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


# include <iostream>
# include <fstream>
# include <sstream>
#include <string>
#include <stdlib.h>
#include <map>

using namespace std;
int main()
{
ifstream infile;
infile.open("prog1_input");
if (!infile.good())
	{
		cout<<" Can't open the file"<<endl;
	}

string header;
getline (infile, header);

map<string, string> ips;
string site;
string IP;
while (infile>>site>>IP)
	{
string input;
cout<<" Enter a URL"<<endl;
cin>>input;
if (site == input)	//check if we found the correct url
{
	cout << IP<<endl;
}else
cout << "Address not found." << endl;
		}      
	

	return 0;
}

1) your code assumes that your site and IP are separated by one or several whitespace characters and soes not contains any excess letters. In your other thread you shown that they are separated by commas and contains quotes around them.

2) You should fill your map in one loop and then search for entered string by using member functon find().
Topic archived. No new replies allowed.