user13268771
user13268771

Reputation:

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?

Upvotes: 2

Views: 83

Answers (1)

Remy Lebeau
Remy Lebeau

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

Related Questions