Lightsong
Lightsong

Reputation: 345

How to extract multiple numbers from a string with numbers of variable length

We have a string which consists of "RESERVE number1,number2,number3,number4" , where number1,number2,number3 and number4 can be any number from 0 to 10, without decimal point.

The string's format cannot be changed, and we want to extract each of those numbers into their respective int variables.

We have tried, so far, to:

Any ideas are welcome!

Upvotes: 0

Views: 164

Answers (1)

P. Saladin
P. Saladin

Reputation: 250

Use sscanf. Example:

int num1, num2, num3, num4;
std::string s = "RESERVE 11,22,33,44";
sscanf(s.c_str(), "RESERVE %d,%d,%d,%d", &num1, &num2, &num3, &num4); 

Upvotes: 1

Related Questions