Hi,

I'm about to start making some little program with input of type string which'll later be converted into numbers, but it has to pass the check is there any non-digit character.

Considering I was lazy to search for functions like that I decided to make my own header for it.

So here it is, what do you think:

stringcheck.h :

#include <cstdlib>
#include <iostream>

using namespace std;

int HasChars(string mystring){
    int length; 
    length=mystring.length()-1;  
    int cti;    (char to int)
    
     while(length>=0){
                   
                    cti=(int)mystring[length]-48;  
                    
                    switch(cti){
                                case 0:
                                case 1:
                                case 2:
                                case 3:
                                case 4:
                                case 5:     
                                case 6:     
                                case 7:     
                                case 8:     
                                case 9:   
                                break;  
                                default: 
                                        return true; 
                                        break;                                             
                                }
                   
                        
                     length--;                                                    
                     
                     } 
					 
  return false;  
     }

So using of it looks like:

string number;
    cin>>number;
 
    
    if (HasChars(number))
    cout<<"It contains characters"<<endl;
    else
    cout<<"Nope, it doesn't contain characters"<<endl;

P.S. Function name root: I was inspired with "isdigit" "isalpha" :D

Wait a minute, so you knew about isdigit but you're too lazy to search for functions? If you're so lazy, why not use isdigit?

As I said, my input is string, and I have to convert it to int array.

But I have to check is there any character in string which ain't 0-9.
But if I'd do this:

string x="12345";
if(!isdigit(x[0]))
cout<<"Contains character"<<endl;

I'd always get result telling it is a char type. I guess it's because string consists of characters, logically.
But I can't convert string to int and then check is it character, because it's will always give me some number ( 0-255 ).
But I can convert it to int, and check is that number part of diapason 0-9, which literally means I got number there. In case I have bigger number than 9, it means I got some of a,b,c, heart, +, or whatever can be character.

By the way, 9th line of my code contains "(char to int)". It's not a code, just accidentally missed part of comment I deleted ( comment's function was to explain "cdi" variable's name ).

Um... you have to check it for all character

//check if str contains only digits
bool isDigits(const std::string& str){
 bool isValid = true;
 for(int i = 0; i < str.size(); ++i){
    if(!isdigit(str[i])){
         isValid = false;
         break;
     }
 }
 return isValid;
}

Note, this code might be ambiguous in some compilers so a better way to do this is with functors

>>So here it is, what do you think:

Honestly, its crap (take it as a helping tip). If you didn't want to use isdigit, then at least use something simpler.

bool isDigit(const std::string& str){
 const std::string digitList = "0123456789";
 bool isValid = false;
 for(int i = 0; i < str.size(); ++i){
    if( digitList.find( str[i] ) == string::npos){ //if not found
         isValid = false;
         break;
    }
 }
 return isValid;
}

@firstPerson

Argh... I've just realized isdigit works fine with strings, I have no idea why I thought it can't work.

Hm.. thanks for the examples. Second one is too much for me, I'd use first one you showed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.