I am looking for a way to pull a value from a C-String with strtok in a specific way. I have a C-String which I need to take out a number and then convert it to a double. I am able to convert to double easily enough, however I need it to only pull one value based on what "degree" is requested. Basically a degree of 0 will pull the first value out of the string. The code I currently has goes through the entire C-string due to the loop I am using. Is there a way to only target one specific value and have it pull that double value out?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char str[] = "4.5 3.6 9.12 5.99";
char * pch;
double coeffValue;
for (pch = strtok(str, " "); pch != NULL; pch = strtok(NULL, " "))
{
coeffValue = stod(pch);
cout << coeffValue << endl;
}
return 0;
}