Reputation:
I am new to C++ . If the user enters a string with different information that is separated by a delimiter, eg #
, and I use string manipulation to extract the info into different variables, how would I check if one of the string variables contain a number? In the simplest way possible (beginner).
eg.
John#Doe#51
Name - John
Surname - doe
Age - 51
How do I make sure that the user typed a number for age?
Upvotes: 2
Views: 83
Reputation: 595772
You can use std::stoi()
or related function checking for errors, or put the extracted string into a std::istringstream
and read an integer from it using operator>>
checking for errors.
Or, just use std::istringstream
to parse the entire delimited string to begin with. No need to extract substrings and convert them manually.
Upvotes: 2