how to input IP address?

Hope this is easy.

I can't seem to find/recall how to have a program accept the input of an IP address/subnet.

For instance:

If I am asking for someones name in the program, i would write something along these lines:

1
2
3
string getName = "";

cout << "Enter Your Name: "



So how do I do this simply to get an IP or something such as a dotted quad? I am pretty sure I would not use string for that, correct?
You can simply input four integers along with three dot characters. (Hints : 7 cin statements in total)
Last edited on
I couldn't do that. part of the program, afterward is having to convert said IP address into 32 bit integer, so it would have to be altogether.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string str_ip = "Please enter a correct IP address : ";
cout << str_ip; cin >> str_ip;

std::stringstream ss(str_ip);
int v = 0, n = 3; uint32_t real_ip;
while(true)
{
    if(ss >> v && n >= 0)
    {
         char c;
         ((uint8_t*)&real_ip)[n] = v;
         ss >> c;
    } else break;
    n--;
}

cout << "The IP address entered : " << real_ip << endl;
Please enter a correct IP address : 124.186.205.63
The IP address entered : 2092617023
Last edited on
Topic archived. No new replies allowed.