I am trying to create a function that takes a string as an input and extracts the first digit as an output. Here is the relevant code. I am new to coding so tips are appreciated.
int extractNum(string id){
int num=0;
bool found=false;
for(int i=0; i<id.length(); i++){
if(isdigit(id[i])){
num=id[i];
found=true;
if(found=true){
break;
}
}
}
return num;
};
The problem I am facing is when ever I pass a example string like "vr2498" it does take the '2' but on assigning the value to a variable or returning the value it changes to 50. Which is incorrect.
Summary: Need to extract first digit from a string.