0

I am trying to break string in different chunks and store these chunks in an string array for further implementation but I'm having some issue in it. For example when I enter 172.16.524.1 it is storing it like 172.16.524.0 Here is My code:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
   string address;
   string str1[4];
   int found,i=0,n1[4],n2,n3,n4;
   cin>>address;
   for(int a=0 ; a<address.length() ; a++)
   {
       found=address.find(".");
       for(int f=0 ; f<found ; f++)
       {
           str1[i]+=address[f];
       }
       i++;
       address.erase(0,found+1);
   }
   for(int j=0 ; j<i ; j++)
   {
       n1[j]=atoi(str1[j].c_str()); //To convert String int to integer
       cout<<n1[j]<<" ";
   }
   cout<<endl;
   return 0;
}
AndyG
  • 35,661
  • 8
  • 94
  • 126
usama97
  • 21
  • 1
  • 5
  • 1
    you haven't inserted your code. – Yvonne Aburrow Dec 01 '17 at 15:42
  • where is your code ? – Pouyan Dec 01 '17 at 15:42
  • Your value of `a` does not change appropriately after each call to `address.find` – AndyG Dec 01 '17 at 15:53
  • Stepping through the program with the debugger that came with your development environment will make very short work of this. A debugger allows you to control the execution of the program and inspect the variables as you go. Do this and keep an eye on `a` and you will find that after erasing chunks of the string, `a` is no longer where you want it to be. – user4581301 Dec 01 '17 at 15:56
  • I recommend you enable: -Wsign-compare, and -Wunused-variable (for n2, n3 and n4) My 'smallest' setting of compiler options is "-std=c++14 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Wcast-align -Wcast-qual -Wconversion -Wpointer-arith -Wunused -Woverloaded-virtual" which auto magically enables more. – 2785528 Dec 01 '17 at 19:24
  • Possible duplicate of [Parse (split) a string in C++ using string delimiter (standard C++)](https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c) – Pouyan Dec 01 '17 at 20:58

3 Answers3

0

You are mixing old-style C strings with new style C++ strings

   found=address.find(".");
   str1[i] = address.substr(0, found) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 
   i++ ;

should do what you want but use of std::vector could be helpful, so the following is somewhat neater

   std::vector<std::string> str1 ;
   ... 
   found=address.find(".");
   str1.push_back(address.substr(0, found)) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 
marom
  • 4,944
  • 8
  • 14
  • a `std::stringstream` and `std::getline` will also work very well. Might not run fast, but will be very easy to write. – user4581301 Dec 01 '17 at 15:54
0
std::string s = "172.168.1.15";
std::string delimiter = ".";
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

Standard way of parsing a string see this question

Ammar Ali
  • 692
  • 5
  • 18
0

I'm having some issue in it

I prefer std::stringstream. The 3rd parameter to std::getline() is a delimiter value, and seems to fit this challenge nicely.

In the following code, I extract each field as a string, and push it into a vector of strings (addrFields).

To report results, I list the fields, then output again with the fields converted to ints with std::stoi().

Note: I append the '.' delimeter to the address ... this trivially avoids the eof() truncating the last field.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>

int exec()
{
   std::string address ("172.16.524.1");
   std::cout << "\n\n  echo of input:     '" << address << "'" << std::endl;

   std::vector<std::string> addrFields; 
   {
      // fill ss with the address    vvv  append one delim
      std::stringstream ss(address + '.');
      std::cout << "  ss contents:       '" << ss.str() 
                << "'" << std::endl;

      do // loop through fields
      {
         std::string f;
         (void)std::getline(ss, f, '.'); // each field ends at delim

         if (false == ss.good())
         {
            if (ss.eof()) break;  // quietly exit, a normal op

            // something wrong with input
            std::cerr << "\n  err Line: " << address << std::endl;

            assert(0); // no use continuing with bad input
         }

         if(f.size()) // ignore blank fields
            addrFields.push_back(f);

         // consider using addrFields.size() 
         //    for special handling 4 or 6 fields
         //    for special handling of a colon appended port num

      } while(true);
   }

   std::cout << "\n  Results  " << std::endl;
   std::cout << "\n      addrFields :   ";

   for (auto f : addrFields)
      std::cout << f << "   ";

   std::cout << "  (strings)   "
             << "\n\n       addr ints :   ";

   for (auto f : addrFields)
      std::cout << std::stoi(f) << "   ";

   std::cout << "  (converted to ints) " << std::endl;

   return 0;
}

Output is:

  echo of input:     '172.16.524.1'
  ss contents:       '172.16.524.1.'

  Results  

      addrFields :   172   16   524   1     (strings)   

       addr ints :   172   16   524   1     (converted to ints) 
2785528
  • 5,162
  • 2
  • 16
  • 18