-1

I'm new to C++, can I ask is there a way to check if a input string is a number? If it is a number, change it to integer.

I know we can use either atoi or stoi.

But how can we create it in a function?

stack
  • 414
  • 1
  • 7
  • 21
  • Hi , actually I'm trying to say that, if user enter a string check if it's a number. If it is, covert to int, else do nothing.. I doubt it's the same – stack Mar 25 '16 at 16:04

5 Answers5

0
  1. Construct a std::istringstream from the std::string.
  2. Extract a number from it. If extraction is successful, use the number. If not, move on to the next thing.
std::string s = "123";
std::istringstream str(s);
int n;
if ( str >> n )
{
   // Extraction is successful.
   // Use n.
}

Further Enahncements

To make your program more robust, you can add a further check to make sure that:

"123FAST" is not treated as an integer.
"123.56" is not treated as an integer.
"123.56xyz" is not treated as an integer.

std::string s = "123";
std::istringstream str(s);
int n;
if ( str >> n )
{
   // Extraction is successful.
   // Add another check.

   // Get the next character.
   // It has to be a whitespace character or EOF for the input
   // to be an integer.
   // If it is neither, don't accept the input as an integer.
   std::istream::int_type c = str.get();
   if ( std::isspace(c) || str.eof() )
   {
      // Use n.
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
-1

You just check each character in your input string is a number, I have a simple example for your problem:

#include <stdio.h>
#include <string.h>

bool is_number(const char * s)
{
    int length = strlen(s);
    for (int i = 0; i < length; i++)
    {
        if (s[i] < '0' || s[i] > '9')
            return false;
    }
    return true;
}

int main()
{
    printf("Enter a string: ");
    char myString[10];
    gets(myString);
    if (is_number(myString))
        printf("%s is a number", myString);
    else
        printf("%s is not a number", myString);
}
No Em
  • 59
  • 5
-1

I don't think you even need to check. According to the C++ API:

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

SEE: http://www.cplusplus.com/reference/cstdlib/atoi/

-

So, you should just be able to do:

// Convert the string
int value = atoi( myString.c_str() );

if ( value == 0 )
{
    // The string was not a valid integer
}
else
{
    // The string was a valid integer
}
Cmoraski
  • 78
  • 4
-1

'isnum()' function below checks if given string is comprised of numbers only. However it might not work if number is very large.

#include <bits/stdc++.h>
using namespace std;
bool isnum(string str){
    for(int i=0;i<str.length();i++){
        if((str[i]<'0') || (str[i]>'9'))return false;
    }
    return true;
    }
int main() {
    // your code goes here
    int n=0,r=1;
    string num;
    cin>>num;
    if(isnum(num)){
        for(int i=num.length()-1;i>=0;i--){
            n+=(r)*(int)(num[i]-'0');
            r*=10;
        }
    cout<<n;
    }
    return 0;
}
Polish
  • 554
  • 1
  • 4
  • 18
  • @stack why downvoted it converts 'string' to 'int' as asked and if its not number than it does nothing – Polish Mar 25 '16 at 18:17
-1

You can check if there is any non-digit character in the string:

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
    std::string test;

    std::cin >> test;
    if(std::find_if(test.begin(), test.end(), [](auto x){return !isdigit(x);}) == test.end())
        std::cout << "its a number" << std::endl;
    else
        std::cout << "not a number" << std::endl;
}
xinaiz
  • 7,744
  • 6
  • 34
  • 78