It is worth noting that there are some strings for which this strategy does not work, like "1xyz2". There are two integers you could parse out of this string. With that in mind, I think there is a more robust solution:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string line;
cout << "Enter the string: ";
getline(cin, line);
stringstream ss(line);
vector<int> myIntegers;
while (!ss.eof())
{
int testInt;
if (ss >> testInt)
{
myIntegers.push_back(testInt);
}
else
{
ss.clear();
ss.ignore(); // extract and discard the non-numeric character
}
}
cout << "The numbers in the strings: " << endl;
for (unsigned int i = 0; i < myIntegers.size(); i++)
{
cout << myIntegers[i] << endl;
}
return 0;
}
// my output below:
Enter the string: 12as 32312dsda sda23asd 1xyz2
The numbers in the strings:
12
32312
23
1
2
Notice however that I did take out one step in your algorithm. While it isn't wrong to separate the strings by spaces, if the integers are really what you want then putting the whole thing into a stringstream and parsing until eof() is true - meaning, there is no more string - is enough.
HTH
How would i adapt my to use your robust algorithm?
i am reading a text file line by line so i cant use while(!ss.eof())
the text file would look like this:
Mickey Mouse S45 78Y W91
Minnie Mouse HH95DF E92DD P88D
Jack Robinson
Jimmy Johnson SSF54S W89W U66T DD44S DD21S
Donald Duck WQ72Q A81B EE89W
and i would want to read one line at a time so i could calculate the sum of the line.
Code:
void getName(ifstream& in, string& firstName, string& lastName)
{
char a;
in >> firstName >> lastName;
//cout << "sucess" << firstName << lastName << endl;
}
void readData(ifstream& in, double& sum, int& count)
{
int i;
int testNumber;
double total;
char testChar;
string line;
vector<string> myStrings; //data container
vector<int> myIntegers;
count = 0;
sum = 0;
getline(in, line); //get data from scores after first lastname as a string, like a sentence.
stringstream ss(line); // load 1 word from string in to stringstream, making it usuable like a cin (Splits string line in to words)
while (ss >> line)
{
myStrings.push_back(line); //while loading stream ss in to string line, put string in to a vector, so storeing each word in string line in the vector
}
for (i = 0; i < myStrings.size(); i++) // i is lower than vector size
{
ss.str(""); //clears contents of ss string
ss.clear(); // clear any error flasts from ss string
ss << myStrings[i];
//ss >> line;
//cout << line << endl;
bool bQuit = false;
while (bQuit == false)
//try to put string in a int, if fail clear bad flag and read from nect char in string till all numbers found. stor number in vector.
{
if (ss >> testNumber) //if string starts with numbers take numbers and store in vector
{ //does not work if starts with letters
//cout << ss.str() << endl;
myIntegers.push_back(testNumber);
bQuit = true;
}
else //if starts with letters clear previous bad flag and read string till hit a number, then exit
{
ss.clear(); //clear bad flag
if (!(ss >> testChar)) bQuit = true; // if string goes in to char then its a letter, exit loop.
}
}
}
for (i = 0; i < myIntegers.size(); i++)
{
//f cout << myIntegers[i] << endl;
if (count == 0)
{
sum = myIntegers[i];
}
else
{
sum += myIntegers[i];
}
++count;
//cout << sum << endl << count;
}
//cin.get();
}
i read the names in the getName() function so the readData() function read the rest of the line.