Hello everyone,
I really need some help with this project I am working on. So I need to go through a text file to search for a particular string (say something like 'play') and then if that word exists in a line I need to find a string after that (say maybe 'try') and copy some numbers after that. I can do the rest but I am having problems searching for the initial string. The sstring 'try' exists in every line and I only want numbers following the string 'try' if the word 'play' also exists in that line. Here is what I have so far

string str_text;

const char searchT[] =  "play";                                             
size_t sizeT = strlen (searchT);

const char search[] = "try";
size_t size = strlen (search);
size_t pos = 0;

vector<double> Q;

while ( getline(infile, str_text).good () )
{

    if( (pos = str_text.find (searchT, 0)) != string::npos )                //Find 'play'.
    {
        str_text = str_text.erase (pos, sizeT) ;
    }

    if ( (pos = str_text.find (search, 0)) != string::npos )                //Find 'try'.
    {
        str_text = str_text.erase (pos, size) ;

        istringstream ins(str_text);
                               ins >> q;



        Q.push_back(q);
    }
}

My biggest problem is I just get the numbers after try every time. I dont want the numbers after try if the word play does not exist in the line.

Edited 3 Years Ago by Dani: Formatting fixed

I forgot to mention that if play exists in the line it is always before the string try.

Hello everyone,
I really need some help with this project I am working on. So I need to go through a text file to search for a particular string (say something like 'play') and then if that word exists in a line I need to find a string after that (say maybe 'try') and copy some numbers after that. I can do the rest but I am having problems searching for the initial string. The sstring 'try' exists in every line and I only want numbers following the string 'try' if the word 'play' also exists in that line. Here is what I have so far

string str_text;

const char searchT[] = "play";
size_t sizeT = strlen (searchT);

const char search[] = "try";
size_t size = strlen (search);
size_t pos = 0;

vector<double> Q;

while ( getline(infile, str_text).good () )
{

if( (pos = str_text.find (searchT, 0)) != string::npos ) //Find 'play'.
{
str_text = str_text.erase (pos, sizeT) ;
}

if ( (pos = str_text.find (search, 0)) != string::npos ) //Find 'try'.
{
str_text = str_text.erase (pos, size) ;

istringstream ins(str_text);
ins >> q;

Q.push_back(q);
}
}


My biggest problem is I just get the numbers after try every time. I dont want the numbers after try if the word play does not exist in the line.

We can't run it because it refers to code that isn't listed and we don't have the input file. However, I am guessing that the problem is that you have two if statements, one after the other, and you probably want a NESTED if-statement. You have this (not nested):

if (/* code to find play */)
{
    // code
}

if (/* code to find try */)
{
    // code
}

I'm guessing you want this instead (nested):

if (/* code to find play */)
{
    // code
    if (/* code to find try */)
    {
        // code
    }
}

I actually thought it should be a nested if - statement like you just mentioned but I get a Debug Assertion failed message with vector subscript out of range. I don't know why that is happening..

This is what I tried,

while ( getline(infile, str_text).good () )
    {

        if( (posT = str_text.find (searchT, 0)) != string::npos )               //Find 'play'.
        {
            str_text = str_text.erase (posT ,sizeT) ;

            if ( (pos = str_text.find (search, posT)) != string::npos )             //Find try
            {
                str_text = str_text.erase (pos, size) ;

                istringstream ins(str_text);
                                                double q;
                ins >> q;

                Q.push_back(q);
            }
        }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Post the whole program and the input file so we can run it please.

Regarding code tags, there are no spaces:

[code=cplusplus] <-- no spaces in between brackets // code

[/code] <-- no spaces in between brackets

Here is my entire code

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

using namespace std;

int main () 
{
    // Input and output files.
    string filename;

    string inputfilename;  
    cout << " Enter filename (input is filename.txt, output is filename.out): ";
    getline( cin, filename );
    inputfilename = filename + ".txt";
    ifstream infile (inputfilename.c_str());

    string outfilename;
    outfilename = filename + ".out";
    ofstream outfile (outfilename.c_str());

    string str_text;

    const char searchT[] = "play";                                              
    size_t sizeT = strlen (searchT);
    size_t posT = 0;

    const char search[] = "try";
    size_t size = strlen (search);
    size_t pos = 0;

    vector<double> Q;

    while ( getline(infile, str_text).good () )
    {

        if( (pos = str_text.find (searchT, 0)) != string::npos )                //Find 'play'.
        {
            str_text = str_text.erase (pos ,sizeT) ;

            if ( (pos = str_text.find (search, pos)) != string::npos )          //Find try.
            {
                str_text = str_text.erase (pos, size) ;

                istringstream ins(str_text);
                double q;
                ins >> q ;

                Q.push_back(q);
            }

            else return 0;
        }
    }

    for (int i=0; i <= Q.size() - 1; i++)
        outfile << Q[i] << "\n";


    outfile.close();
    infile.close();

    return 0;
}

I added the else return 0; to get rid of the assertion error but nothing prints in the output file. However if I do the folowing loop in the middle instead i do get the desired number i want but it is for the entire data instead of the lines with just play in it.

while ( getline(infile, str_text).good () )
{

    if( (pos = str_text.find (searchT, 0)) != string::npos )                //Find 'play'.
    {
        str_text = str_text.erase (pos ,sizeT) ;
                      }

        if ( (pos = str_text.find (search, pos)) != string::npos )          //Find try.
        {
            str_text = str_text.erase (pos, size) ;

            istringstream ins(str_text);
            double q;
            ins >> q ;

            Q.push_back(q);
        }


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Attachments
random 0 id 0x6989ad29f9e1 lime 46375959485519783
	corner=32 t1=1.02144 t2=0.953973 ta=0.952422 bulk=230.145
	height 4.53606 4.28258 156.748 156.643 104.397 216.752 90.7249 91.323
	try 225495 256222 282080 300904 306807 593073 7.39766e+06 7.67218e+06
	line: fangle=-0.327709 t0=-217.874 dof=4 pop2=5697.86
	crying-1.37359 t0=-279.504 dof=4 pop2=12087.1
	hello 01=-3.09692 02=-1.28732 03=-0.432671 12=0.891123 13=1.02265 23=1.13192

random 1 id 0x4e4d3084ec75 lime 46375959485791394
	corner=224 t1=0.953424 t2=1.02286 ta=0.954428 bulk=228.742
	height 47.6833 83.9673 63.7317 178.775 237.62 251.998 206.092 24.3245
	try 136001 195383 232108 290404 2332.12 3771.27 113178 211037
	line: fangle=0.20359 t0=-270.019 dof=3 pop2=1670.4
	crying8.16115 t0=-356.906 dof=3 pop2=3661.42
	hello 01=-1.16132 03=0.119499 13=0.837976

play 0x8e517ada34bd 1 16.25 0.966996 1.04074 3.67293
random 2 id 0x8e517ada34bd lime 46375982831368174
	corner=145 t1=1.03887 t2=0.970132 ta=1.01593 bulk=229.372
	height 78.7859 77.729 0.0222686 1.38352 93.1585 94.5421 3.96256 2.97254
	try 162674 166540 76992.1 111475 1.01454e+06 975880 530105 584127
	line: fangle=-0.104561 t0=-287.666 dof=4 pop2=53.0206
	crying-1.76834 t0=-367.405 dof=4 pop2=1025.08
	hello 01=-0.00462306 02=-0.119666 03=-0.0127602 12=-0.181022 13=-0.0204453 23=-0.389337

play 0x6989ad29f9e1 0 27.6875 0.959854 1.02001 2.03838
random 3 id 0x6989ad29f9e1 lime 46375982831403167
	corner=107 t1=0.953973 t2=1.02144 ta=1.00261 bulk=229.166
	height 141.174 79.0533 78.6518 177.084 95.8596 106.909 63.6327 77.7938
	try 205862 302838 331933 471159 268972 360801 2.19046e+06 2.33705e+06
	line: fangle=0.0511734 t0=-230.493 dof=4 pop2=1633.74
	crying-0.0768642 t0=-292.124 dof=4 pop2=5453.62
	hello 01=-0.932112 02=-0.651756 03=0.0826435 12=-0.314258 13=0.636946 23=1.4271

random 4 id 0x4e4d3084ec75 lime 46376139316516550
	corner=10 t1=0.953424 t2=1.02286 ta=1.02367 bulk=230.079
	height 69.8183 60.6293 195.106 218.509 165.194 236.886 196.096 228.878
	try 155052 244286 215090 287543 1849.15 1718.43 124287 183660
	line: fangle=-0.580494 t0=-169.164 dof=3 pop2=5008.59
	crying-0.280319 t0=-246.358 dof=3 pop2=12192.1
	hello 01=-2.94397 03=-0.726107 13=0.518009

play 0x163eb2508655 1 18.875 0.877971 0.952036 4.80209
random 5 id 0x163eb2508655 lime 46376139316644606
	corner=130 t1=0.949236 t2=0.88762 ta=0.921974 bulk=227.084
	height 56.1787 55.9804 110.37 111.055 189.528 209.822 27.1669 75.2979
	try 40935.5 52230.7 273604 370387 1173.54 2565.56 869449 1.07348e+06
	line: fangle=0.248439 t0=-268.221 dof=3 pop2=3687.69
	crying11.4548 t0=-383.485 dof=3 pop2=6678.84
	hello 01=-1.55419 03=0.124743 13=0.913421

random 6 id 0x8e517ada34bd lime 46376168963789768
	corner=231 t1=1.03887 t2=0.970132 ta=1.03844 bulk=229.118
	height 77.6155 76.8721 3.26735 1 87.1562 88.8529 2.81469 4.99621
	try 178150 214064 61879.9 73912.5 1.10016e+06 1.11383e+06 487497 567727
	line: fangle=-0.0867544 t0=-292.875 dof=4 pop2=52.5755
	crying-1.54153 t0=-372.559 dof=4 pop2=1003.55
	hello 01=0.017495 02=-0.101209 03=0.00460952 12=-0.164518 13=-0.00756011 23=-0.368138

play 0x163eb2508655 1 18.9375 0.889542 0.948134 4.18238
random 7 id 0x163eb2508655 lime 46376168963958705
	corner=192 t1=0.949236 t2=0.88762 ta=0.950504 bulk=226.984
	height 55.7697 251.858 151.801 162.597 80.0579 57.8028 58.7975 56.8572
	try 34296.5 40169.3 123106 166011 784.711 2314.67 470166 622840
	line: fangle=0.172062 t0=-248.241 dof=3 pop2=5831.78
	crying11.0038 t0=-363.534 dof=3 pop2=9805.42
	hello 01=-2.09482 03=0.0165084 13=1.00831

random 8 id 0x4e4d3084ec75 lime 46376202427964110
	corner=5 t1=0.953424 t2=1.02286 ta=1.02471 bulk=230.108
	height 84.651 116.526 72.5781 50.8398 248.689 231.77 78.2498 241.391
	try 130882 241866 212301 270920 1615.92 2022.21 124430 242606
	line: fangle=0.0650508 t0=-256.146 dof=3 pop2=223.214
	crying4.69175 t0=-337.405 dof=3 pop2=1611.56
	hello 01=-0.433896 03=0.0343109 13=0.296953

random 9 id 0x163eb2508655 lime 46376202428170175
	corner=186 t1=0.88762 t2=0.949236 ta=0.892656 bulk=225.592
	height 70.0272 70.5991 1.738 0.318749 79.3379 47.0604 77.527 77.7354
	try 49820.2 64239.8 155557 189234 1147.59 2338.4 1.17852e+06 1.38535e+06
	line: fangle=-0.000274567 t0=-272.602 dof=3 pop2=9.95729
	crying3.92162 t0=-375.959 dof=3 pop2=1225.14
	hello 01=-0.0939443 03=-0.00670218 13=0.0342799

play 0x163eb2508655 1 19.1875 0.883819 0.94956 4.0432
random 10 id 0x163eb2508655 lime 46376361044759123
	corner=180 t1=0.949236 t2=0.88762 ta=0.952516 bulk=227.171
	height 41.2738 65.9414 45.2538 47.0427 251.874 66.705 219.804 6.01063
	try 29965.4 42281.4 114117 137271 874.398 10652.9 477931 608569
	line: fangle=0.231443 t0=-301.056 dof=3 pop2=1674.54
	crying10.0528 t0=-413.751 dof=3 pop2=4026.82
	hello 01=-0.983277 03=0.148089 13=0.679549

random 11 id 0x6989ad29f9e1 lime 46376361044912796
	corner=9 t1=1.02144 t2=0.953973 ta=0.952798 bulk=229.179
	height 75.2167 39.7413 17.7955 151.92 1.73995 222.33 130.65 131.303
	try 223563 287175 302702 394073 290682 429934 9.46199e+06 9.86957e+06
	line: fangle=-0.397237 t0=-256.156 dof=4 pop2=210.57
	crying-3.0067 t0=-317.787 dof=4 pop2=1507.62
	hello 01=-0.627263 02=-0.218377 03=-0.455029 12=0.273848 13=-0.360948 23=-0.888266

play 0x4e4d3084ec75 1 17.8125 0.953303 1.03443 3.485
random 12 id 0x4e4d3084ec75 lime 46376370016888607
	corner=61 t1=1.02286 t2=0.953424 ta=0.95615 bulk=230.775
	height 94.9418 85.2665 250.906 23.5926 252.891 187.24 75.8206 184.951
	try 140514 203926 234180 299848 1427.65 3494.69 150727 225846
	line: fangle=0.0555943 t0=-257.387 dof=3 pop2=16.6698
	crying3.87909 t0=-337.122 dof=3 pop2=915.519
	hello 01=-0.0807566 03=0.0471938 13=0.118968

play 0x8e517ada34bd 0 16.9375 0.973587 1.04162 2.24597
random 13 id 0x8e517ada34bd lime 46376370017077280
	corner=174 t1=0.970132 t2=1.03887 ta=0.985101 bulk=228.21
	height 114.785 115.715 0.631028 0.739748 125.302 124.181 32.2971 32.8861
	try 162234 185234 107223 132898 1.12198e+06 1.13863e+06 528038 539393
	line: fangle=-0.11871 t0=-256.839 dof=4 pop2=396.119
	crying-2.78652 t0=-336.622 dof=4 pop2=356.426
	hello 01=0.502532 02=-0.0944531 03=0.0529093 12=-0.412845 13=-0.371734 23=-0.466178

play 0x6989ad29f9e1 1 28.75 0.949023 1.02737 3.95053
random 14 id 0x6989ad29f9e1 lime 46376472430179117
	corner=131 t1=1.02144 t2=0.953973 ta=0.990602 bulk=230.763
	height 136.047 120.676 158.918 163.337 88.0011 87.087 157.216 157.645
	try 218909 250304 279713 362657 315449 481958 6.77552e+06 7.13147e+06
	line: fangle=-0.0785077 t0=-184.038 dof=4 pop2=1344.24
	crying1.42236 t0=-245.669 dof=4 pop2=4053.17
	hello 01=-1.51401 02=-0.252525 03=-0.1943 12=1.26608 13=0.526583 23=-0.0877081

random 15 id 0x4e4d3084ec75 lime 46376472430331841
	corner=13 t1=0.953424 t2=1.02286 ta=1.02369 bulk=229.11
	height 2.23324 228.933 10.1484 171.201 249.611 233.82 148.007 237.565
	try 144200 178256 225484 310561 1792.67 2631.97 148278 258639
	line: fangle=-0.763304 t0=-265.905 dof=3 pop2=76.9513
	crying-6.61889 t0=-332.19 dof=3 pop2=3314.11
	hello 01=-1.05626 03=-0.781353 13=-0.627143

play 1x4e4d3084ec75 1 17.9375 0.951757 1.02745 3.51899
random 16 id 0x4e4d3084ec75 lime 46376617696554072
	corner=157 t1=1.02286 t2=0.953424 ta=1.00813 bulk=230.329
	height 131.817 95.7132 46.6171 242.829 229.698 77.3839 86.1207 91.5889
	try 134671 186439 213243 277802 1319.03 3019.4 155997 328651
	line: fangle=0.0678556 t0=-249.795 dof=3 pop2=93.8232
	crying4.39579 t0=-330.444 dof=3 pop2=1235.75
	hello 01=-0.255625 03=0.0479261 13=0.218204

play 0x6989ad29f9e1 0 28.875 0.963634 1.01707 1.98122
random 17 id 0x6989ad29f9e1 lime 46376617696734681
	corner=86 t1=0.953973 t2=1.02144 ta=1.01056 bulk=229.209
	height 27.311 100.114 141.1 152.019 106.63 106.568 32.9118 33.2839
	try 241961 273695 253180 349523 313492 445919 2.42913e+06 2.70372e+06
	line: fangle=0.0223674 t0=-234.328 dof=4 pop2=6469.55
	crying1.24429 t0=-295.959 dof=4 pop2=12900.2
	hello 01=-2.66443 02=-1.16927 03=-0.0300159 12=0.630645 13=1.40902 23=2.0556

play 0x163eb2508655 0 19.3125 0.891245 0.939687 2.95782
random 18 id 0x163eb2508655 lime 46376618688946849
	corner=113 t1=0.88762 t2=0.949236 ta=0.917298 bulk=225.911
	height 75.1601 78.064 68.7023 12.7078 70.5072 70.3564 58.8958 54.0734
	try 53973.3 80936.8 163591 219386 1206.26 3047.17 1.12849e+06 1.24902e+06
	line: fangle=0.122987 t0=-280.592 dof=3 pop2=105.427
	crying6.45775 t0=-387.481 dof=3 pop2=1429.84
	hello 01=-0.181805 03=0.102072 13=0.235424

random 19 id 0x8e517ada34bd lime 46376618689028355
	corner=40 t1=0.970132 t2=1.03887 ta=1.03551 bulk=227.77
	height 51.3702 51.7928 1.54919 130.628 67.6632 69.7311 190.158 190.779
	try 162048 187601 61377.2 80904.9 967026 974457 451731 489807
	line: fangle=-0.410057 t0=-253.338 dof=4 pop2=6404.43
	crying-9.98046 t0=-334.029 dof=4 pop2=9499.37
	hello 01=-0.297233 02=-0.128098 03=-1.31627 12=-0.0378935 13=-2.27869 23=2.86909

random 20 id 0x6989ad29f9e1 lime 46376678374379796
	corner=237 t1=1.02144 t2=0.953973 ta=1.02648 bulk=229.852
	height 28.2822 176.158 158.521 110.92 99.9596 101.66 124.226 124.724
	try 184442 242667 336668 309391 414336 443810 7.44143e+06 7.79149e+06
	line: fangle=-0.457551 t0=-224.807 dof=4 pop2=2345.09
	crying-3.65499 t0=-286.438 dof=4 pop2=7170.2
	hello 01=-2.19186 02=-1.10646 03=-0.514192 12=0.200169 13=0.402222 23=0.570066

random 21 id 0x4e4d3084ec75 lime 46376678374412224
	corner=201 t1=1.02286 t2=0.953424 ta=1.0263 bulk=230.855
	height 50.2872 22.6545 71.7112 130.228 242.814 253.477 219.52 22.6979
	try 169365 198322 214221 266529 1609.21 2127.55 108349 233262
	line: fangle=0.107255 t0=-288.754 dof=3 pop2=2728.4
	crying7.61129 t0=-375.414 dof=3 pop2=5451.06
	hello 01=-1.63715 03=-0.000216959 13=0.918024

play 0x6989ad29f9e1 1 28.9375 0.948786 1.02663 3.90867
random 22 id 0x6989ad29f9e1 lime 46376792409463691
	corner=130 t1=1.02144 t2=0.953973 ta=0.989601 bulk=230.507
	height 68.6953 65.1139 154.372 149.685 97.8971 119.53 115.367 122.138
	try 227740 279713 338078 354767 391202 696495 1.74311e+07 1.76661e+07
	line: fangle=-0.159287 t0=-207.281 dof=4 pop2=2790.1
	crying0.128923 t0=-268.912 dof=4 pop2=7279.02
	hello 01=-2.16018 02=-0.76621 03=-0.251533 12=0.911879 13=0.79105 23=0.690679

random 23 id 0x4e4d3084ec75 lime 46376792409466289
	corner=21 t1=0.953424 t2=1.02286 ta=

I used the code before you edited it. That's a pretty long input file. What should the program do with an input file like this?

hi buy play 5 3 2 try 7 8 9

well there are two situations
1) ......random hi buy 5 3 2 try 7...... random hi and
2)....... random hi buy play 5 3 2 try 6 8 9 ........ random hi

I want the number right after try i.e 6 in this case (basically case (2)) when there is 'play' before the 'try' before the next random comes along... in situation (1) there is no 'play' so I don't want the number 7 after the 'try'.

I hope that helps and thank you very much for looking into this as I have spent the whole day to no avail so finally decided to get some help.

well there are two situations
1) ......random hi buy 5 3 2 try 7...... random hi and
2)....... random hi buy play 5 3 2 try 6 8 9 ........ random hi

I want the number right after try i.e 6 in this case (basically case (2)) when there is 'play' before the 'try' before the next random comes along... in situation (1) there is no 'play' so I don't want the number 7 after the 'try'.

I hope that helps and thank you very much for looking into this as I have spent the whole day to no avail so finally decided to get some help.

Okay, for the input file I provided, what should str_text contain AFTER line 46 below is executed?

str_text = str_text.erase (pos, size) ;

given the input line:

hi buy play 5 3 2 try 7 8 9

I was hoping it would just contain '7 8 9 ........' after line 46 is run.

I was hoping it would just contain '7 8 9 ........' after line 46 is run.

I'm guessing that you need to change your two erase lines on lines 42 and 46 to the following:

str_text = str_text.erase (0 , pos + sizeT) ; // line 42
str_text = str_text.erase (0 , pos + size) ; // line 46

Add a cout line after line 46 to make sure you get " 7 8 9".

This question has already been answered. Start a new discussion instead.