C++ How do I scan a string for particular characters?

I stored 3 lines of strings from a file into a variable called temp. I did this by converting the 3 lines into integers. I need to know whether the strings contained certain characters like “:” or “~” or “b.” How do I do this if the strings were saved as integers?

This is my code.

//Variables
string temp; //String data from file, to be converted to an int
int test[] = {0,0}; //0 for first line, 1 for second

//Get data from file
for(int i = 0; i <= 2; i++)
{
if(!testfile.eof())
{
getline(testfile,temp); //Get 1 line from the file and place into temp
test[i] = atoi(temp.c_str()); //set the test variable to the int version of the line
}

}

✅ Answers

? Best Answer

  • for this to work you have to include <sstream>

    stringstream ss(temp);
    ss >> test[i];

    string temp; //String data from file, to be converted to an int
    int test[] = {0,0}; //0 for first line, 1 for second

    //Get data from file
    for(int i = 0; i <= 2; i++)
    {
    if(!testfile.eof()){
    getline(testfile,temp); //Get 1 line from the file and place into temp
    stringstream ss(temp);
    ss >> tst[i];
    }
    }

  • Leave a Comment