I have to count the number of integers in a string like this "1.1 some text. 1.2 some text", where it should return 0, as there are no integers, but I still get 2 on the output.
Here is the code:
Expand|Select|Wrap|Line Numbers
- void IntegerNumber()
- {
- int NumberOfIntegers = 0;
- string SubString;
- int flag;
- while (!file.eof())
- {
- getline(file, SubString, ' ');
- flag = 0;
- for (int i = 0; i < SubString.length(); i++)
- {
- if (SubString[i] >= '0' && SubString[i]<= '9') flag = 1;
- else flag = 0;
- }
- if (flag == 1) ++NumberOfIntegers;
- }
- cout << "The number of Integers = " << NumberOfIntegers << endl;
- }
Thanks a lot!