The following code snippet is supposed to run through a vector of strings (words), and if the string contains an integer, it should push the int to an integer vector (nums) that contains only those numbers.
for (int i = 0; i < words.size(); i++){
for(int j = 0; j < words.at(i).size(); j++){
if (isdigit(words[i][j])){
nums.push_back(words[i][j]);
}
}
cout << words.at(i) << endl;
}
I've been given a file with the words on it. My code successfully pulls the words from the file and places them in a vector. However, my isdigit() function is not returning the expected values. The words are ho1d h4m, dance2, 8est, next, di6est, se3d, tes7 and my expected value for the nums vector is 1,4,2,8,6,3,7. Here's my print snippet:
for (int i = 0; i < nums.size(); i++){
cout << nums.at(i) << " ";
}
But it's returning the values 49 52 50 56 54 51 55. EDIT: Just recognized that these are the ascii values. How do I prevent them from pushing the ascii values instead of the integers? I've even tried to use the get() function and run through every character instead of every line while reading the input, and I still get the same values returned. Where am I going wrong?