parse a string contains number and characters in c++ from file

hello!!
I have a text file which contains many sentences. I am trying to extract only the numerical values from the string that conatins characters,numbers and white spaces from the file. I want to seperate the characters and numbers into different parts.


for example: the file contains sentances as given below.

I have to go to school for 10min.
You will come here in 15min.
He stayed here for 20min.


from the above sentances, I want to seperate " I have to go to school for " and "10" and put them into two different variables and also 10 should be in integer format.

Please help me out !!
Thanks in advance!!


1. Read each line into a std::string variable (e.g. using std::getline)
2. Loop through the string until you get to a number
3. Use the .substr() member function to get only the part of the string before the number, and store it in another std::string
4. Trim the string so that it starts with the number
5. Use std::istringstream to read the number into a numeric variable
6. On the same std::istringstream, use std::getline to read the rest of the string into step 1, and repeat the whole process

You will want to make use of std::vector for storing the various strings and numbers.
Topic archived. No new replies allowed.