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?
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?
std::istringstream
from the std::string
.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.
}
}
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);
}
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
}
'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;
}
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;
}