0

I'm trying to write a function that checks if a string contains a number, positive or negative.

getline(cin,line);

    istringstream sin(line);

    while (sin >> ws >> in){

        if(all_of(in.begin(), in.end(), ::isdigit){
        //do something with number
        }

Problem is, isdigit() will set all negative numbers as not numbers. What other ways are there to check whether a string contains numbers?

Sample input: 1 2 -2 asdf 4.

adrianp
  • 173
  • 1
  • 3
  • 17

1 Answers1

0

Check if the leading character is - (or +, or whatever else you'll accept), and if so, remove it, then use your current test for what remains.

A fuller solution would be to use a regular expression.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101