1

I have a trouble to remove all the characters between the first parenthesis "(" and the last parenthesis "(" including them. Here is the test program I use to make it work, but without success...

#include <iostream>
#include <string>

using namespace std;

int main()
{

    string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";

    int count = 0;

    for (std::string::size_type i = 0; i < str.size(); ++i)
    {

        if (str[i] == '(')
        {
            count += 1;
        }

        cout << "str[i]: " << str[i] << endl;

        if (count <= 4)
        {

            str.erase(0, 1);
            //str.replace(0, 1, "");

        }

        cout << "String: " << str << endl;

        if (count == 4)
        {
            break;
        }

        cout << "Counter: " << count << endl;

    }


    cout << "Final string: " << str << endl;

    system("PAUSE");

    return 0;
}

In the example I have showed above, my target is (at least) to get the string:

"1.32544e-7 0 0 ) ) )"

which is extracted from original string

"( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )"

To be more precise, I want to extract the value

"1.32544e-7"

and convert to double in order to use in calculations.

I have managed successfully to remove the

" 0 0 ) ) )"

since it is a kind of constant value.

Thank you!

  • Declare a variable equal to the strings length and then make a function to delete the 1st and second to last character of the string – Orfeo Terkuci Jun 15 '16 at 11:26
  • Is the format of the string always the same? – Some programmer dude Jun 15 '16 at 11:28
  • Also there is no need to remove the last part of the string, once you find the start of the number you want to extract, a simple call to e.g. ['std::stod`](http://en.cppreference.com/w/cpp/string/basic_string/stof) will suffice. – Some programmer dude Jun 15 '16 at 11:29
  • String format is always the same, but variable part of the string is "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 " (numbers change), therefore I needed to catch the last "(". Thank you! – B. Sekutkovski Jun 15 '16 at 11:57

2 Answers2

6

Rephrasing the problem as "I want to extract the double immediately following the last '('", a C++ translation is pretty straightforward:

int main()
{
    string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";

    // Locate the last '('.
    string::size_type pos = str.find_last_of("(");

    // Get everything that follows the last '(' into a stream.
    istringstream stream(str.substr(pos + 1));

    // Extract a double from the stream.
    double d = 0;
    stream >> d;                       

    // Done.        
    cout << "The number is " << d << endl;
}

(Format validation and other bookkeeping left out for clarity.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

You are looping from 0 to the length of the string and erasing charterers as you go, which means you don't look at each of them.

A small change will get you part way there. Don't change the string you are trying to iterate over, just remember the index you got to.

using namespace std;
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";

int count = 0;
std::string::size_type i = 0;
//^--------- visible outside the loop, but feels hacky
for (; i < str.size(); ++i)
{
    if (str[i] == '(')
    {
        count += 1;
    }

    cout << " str[i]: " << str[i] << endl;

    //if (count <= 4)
    //{
        //str.erase(0, 1);
    //}
    //^----------- gone

    cout << "String: " << str << endl;

    if (count == 4)
    {
        break;
    }

    cout << "Counter: " << count << endl;
}

return str.substr(i);//Or print this substring

This leaves us on the forth opening brackets, so we need an extra increment if we don't get to the end of the string.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • @molbdnilo's answer is, as you can see, much neater. I wanted to emphasise that changing something while you iterate over it is often the cause of trouble, so showed a minimal change to fix it. – doctorlove Jun 15 '16 at 12:01
  • Thank you for pointing out to the cause of the problem, this is very helpful for understanding the code much better, since I have lost one week to solve this (seemingly) simple problem... – B. Sekutkovski Jun 15 '16 at 12:10