mercredi 8 novembre 2017

Searching string for a value input by the user

I have an issue in this part of my code. If I search for "Grid receiver 5" for example, instead of the variable gridRec, the if statement tests as true and the code underneath is executed.

Despite gridRec containing "Grid receiver 5" in the correct format, it will not execute the code.

state and npos are always the value they should be.

I am searching for these particular phrases in a large string of data.

        string strInput;

        string userInput1;

        cin >> userInput1; // stored number input by user

        string gridRec = "Grid receiver ";

        gridRec.append(userInput1); // adds number to end of gridRec String

        getline(inf, strInput); // opens large string of data

        //If Grid Receiver is found and state is IDLE (0)
        if ( std::string::npos != strInput.find(gridRec) && state == IDLE)
        {
            state = FOUND_GRID_RECEIVER;
            printf ("Found Grid");

        }

Is it possible to use variables to find text in a string? Is there a more efficient way to carry out this function?

Many thanks in advance.

Aucun commentaire:

Enregistrer un commentaire