you need to do some work here.
Lets say you have an array of 30 chars which holds the string. Check the array piece by piece for any number. the first number you encounter, convert it to a number using "atoi()" or "atof()" in a variable. After continuing checking the array, if the program encounters a number next to the last one, it will convert it to a number and it will add it to the variable.
and so on...
For the float numbers, you will need to also check each "." character and do the appropriate functions.
I am not in front of xcode now, so, I will get back to you when i have the code.
EDIT: I just thought something simpler. You can just check your sentence for two spaces (isolate each word and see if it is integer or float).
I think I have some code ready...
Code:
#include <iostream>
#include <vector>
using namespace std;
vector<float> v;
float checkNumber(char *p){
return atof(p);
}
void check(char *str){
int length = strlen(str);
int i,j;
char token[length];
for(i=0; ; i++){
for(j=0; (str[i]!=' ' && str[i]); j++, i++)
token[j] = str[i];
token[j] = '\0';
cout << token << "\n";
if(checkNumber(token)){
cout << "isnumber! " << checkNumber(token) << "\n";
v.push_back(checkNumber(token));
}
if(!str[i]) break;
}
}
int main (int argc, char * const argv[]) {
char p[] = "95 he 89 llo 12.54 helllo again 1254 3894 ok 90";
check(p);
cout << "vector contents:\n";
for(int i=0; i<v.size(); i++){
cout << v[i] << "\n";
}
return 0;
}
The only problem is that it doesn't recognize the number 0; But I think it can easily be fixed with an 'if' statement... It would be easier to do it with the use of strings, but I figured you wanted it to use it with arrays...
EDIT: It will recognize numbers no matter what type there are, float, integers, and no matter how many digits they contain. try it