2

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?

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Does this answer your question? [How to determine if a string is a number with C++?](https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c) – ChrisMM Apr 21 '20 at 16:11

1 Answers1

2

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.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620