New Posts  All Forums:Forum Nav:

c++ strings n stuff

post #1 of 5
Thread Starter 
hy, i am coding a small app that will ping all ip on my local network, but i am having some problems with strings.. the output of this code is:
192.168.1.1 connected
192.168.1.12 connected
192.168.1.123 connected

the results are not correct, if i ping manually none of the addresses responds (except 192.168.1.1). can you help me find the error?


Code:
#include <iostream>
#include <Windows.h>
#include <string>
#include <sstream>

using namespace std;

int main()
{cout<<"Ping all adresses on local network from 192.168.1.1 to 192.168.1.255\
\
Press enter to start...";

string ip;
stringstream out;
int a;

for(int i=1;i<255;i++)
{
ip="192.168.1.";
out << i;
ip+=out.str();

out.clear();

a=system(("ping -n 1 "+ip+" >> nul:").c_str());

if(a==0)//ping returns 0 if success
{
cout<<"\
"<<ip<<"connected";
}


ip.clear();
}
system("pause");
return 0;
}
monster
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II 965 Gigabyte 990xa UD3 Club3D 4870 512mb 8gb gSkill sniper 1600 
Hard DriveOptical DriveOSMonitor
640 raid 0 + 500gb samsung f3 Philips win 7 x64 Samsung 24" + 19" 
PowerCaseMouse
seasonic 520W Thermaltake Mambo Razer copperhead 
  hide details  
Reply
monster
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II 965 Gigabyte 990xa UD3 Club3D 4870 512mb 8gb gSkill sniper 1600 
Hard DriveOptical DriveOSMonitor
640 raid 0 + 500gb samsung f3 Philips win 7 x64 Samsung 24" + 19" 
PowerCaseMouse
seasonic 520W Thermaltake Mambo Razer copperhead 
  hide details  
Reply
post #2 of 5
Maybe check out this, could have what your missing, gl
Edited by Asmodean - 3/10/11 at 2:17pm
post #3 of 5
clear() does not reset the stringstream to an empty string. That's why your code is failing.

The long explanation and what you need to do instead is here.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #4 of 5
1st of all clear() is not emptying, 2nd I know you may think of this as a much harder approach but do it properly, do not use system call, use IcmpSendEcho2 it's rather easy to implement.
post #5 of 5
Thread Starter 
Quote:
Originally Posted by error10 View Post
clear() does not reset the stringstream to an empty string. That's why your code is failing.

The long explanation and what you need to do instead is here.
rep++
the code still has some errors but i 'll fix it soon
monster
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II 965 Gigabyte 990xa UD3 Club3D 4870 512mb 8gb gSkill sniper 1600 
Hard DriveOptical DriveOSMonitor
640 raid 0 + 500gb samsung f3 Philips win 7 x64 Samsung 24" + 19" 
PowerCaseMouse
seasonic 520W Thermaltake Mambo Razer copperhead 
  hide details  
Reply
monster
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II 965 Gigabyte 990xa UD3 Club3D 4870 512mb 8gb gSkill sniper 1600 
Hard DriveOptical DriveOSMonitor
640 raid 0 + 500gb samsung f3 Philips win 7 x64 Samsung 24" + 19" 
PowerCaseMouse
seasonic 520W Thermaltake Mambo Razer copperhead 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming