Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Forgive Me I'm New To The C++ Scene Im Trying To Make A Program Were The User Can Enter There Ip Into Batch File To Look For Alive Ports Anyways When I Enter And Ip Like 172.19.Etc I Only Get 172

C++
string ip;
	int anwswer;
	int price=0;
  cout << "Enter IPv4: ";
  getline (cin,ip);
  stringstream(ip) >> price;
  cout << price <<"\n" <<"Is this right? \n";
  cin>>i;
  
  {
	int if(i == "y";

	  cout<<"DONE!"; //place in to batch file
	  else NULL; //goto main
  }
 
  system("pause");
  return 0;
Posted
Updated 7-Aug-14 6:19am
v4

That's the correct behaviour of the stringstream's extraction operator: you requested an int and it provided the first valid int represented in the string.
If you want to extract the four ip octects as integers then try the following code:
C++
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
  string ip;
  int anwswer;
  int octet[4];
  char c;

  do
  {
    cout << "Enter IPv4: ";
    getline (cin,ip);
    stringstream s(ip);
    for (int n=0; n<4; ++n)
      s >> octet[n] >> c;

    for (int n=0; n<4; ++n)
      cout << "octet[" << n << "]=" << octet[n] << endl;

    cin>>c;
    if(c == 'y')
      cout<<"DONE!"; //place in to batch file
    else
      cout<<"NOT DONE!"; /
  } while (c != 'y');

  return 0;
}
 
Share this answer
 
v2
Replace ...

C++
stringstream(ip) >> price;
cout << price <<"\n" <<"Is this right? \n";


... with ...

C++
in_addr_t ip = inet_addr(ip.c_str());
cout << inet_ntoa(ip) <<"\n" <<"Is this right? \n";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900