简体   繁体   中英

How can I check if the data in a string is a number on C++?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM