Tuesday, July 14, 2009

C++ Data Types?

Lets say I want to do something like this:





#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;sstream%26gt;


#include %26lt;string%26gt;





const char *ip





int main(int argc, char *argv[])


{





std::cin %26gt;%26gt; ip





system("telnet" + ip);





}





what would the variable type of 'ip' have to be for this to work, and if it has to be converted from a certain type, how would I go about doing so?

C++ Data Types?
If you want to use direct string additions etc it would be better to use std::string and then convert it to character point for the system call as it wont support the string type





Here is the equivalent code








#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





int main(int argc, char *argv[])


{


std::string ip;


std::cin %26gt;%26gt; ip;





ip = "telnet "+ip;


system(ip.c_str());





}








Run the program and enter the ip address


$a.out


127.0.0.1


Trying 127.0.0.1...


Connected to 127.0.0.1 (127.0.0.1).


Escape character is '^]'.


login: ^[[A^D


Password:
Reply:There isn't any variable type for ip because it has more than 1 dot. Try using 4 variables and concatenating them system("telnet" + ip1 + "." + ip2 + "." + ip3 + "." + ip4) and integer should be ok.


You could also use a char array.


No comments:

Post a Comment