user2766873
user2766873

Reputation: 45

C++ how to convert string to char array and check for digits?

how do I do this? i want it to check for numbers

cout<<"Enter your first number: ";
std::cin >> dblNumOne;
int i=0;
char str[]=dblNumkOne;
while (str[i])
{
    if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
    else printf ("character %c is not alphabetic\n",str[i]);
    i++;
}

Upvotes: 1

Views: 2258

Answers (4)

James
James

Reputation: 1019

If you want to check for numbers, you could try this:

std::string str = "";
std::cout<<"enter string>>";
std::cin>>str;

int is_number = 0; 
char filter[] = "0123456789";

for(int n=0; n<str.size(); ++n){
   for(int i=0; i<10; ++i){

      if(str[n] == filter[i]){
          ++is_number;
          break; // found match, filter next char in str
      }else if(str[n] != filter[i] && i == 9){
          std::cout<<"'"<<str[n]<<"' is not a digit!\n";
      }
   }
}

if(is_number == str.size()){
    std::cout<<"You entered a number";
}else std::cout<<"You didn't enter a number!";

or to recognize decimal numbers you could try this:

int is_number = 0, is_decimal = 0; 
char filter[] = ".0123456789";

for(int n=0; n<str.size(); ++n){
   for(int i=0; i<11; ++i){

      if(str[n] == filter[i]){
          if(filter[i] == '.'){
            ++is_decimal;
          }
          ++is_number;
          break;
      }
   }
}// for decimal

if(is_number == str.size() && is_decimal <= 1){
    std::cout<<"You entered a number";
}else std::cout<<"You didn't enter a number!";

Upvotes: 0

JaneGoodall
JaneGoodall

Reputation: 1506

1) Convert the string to a char array:

strcpy(charArray,stringArray.c_str());

Source: http://v2.cplusplus.com/forum/windows/71633/

2) Print out whether it's a digit or not using isdigit:

while (charArray[i]) {
    if (isdigit(charArray[i])){
        printf ("character %c is a digit\n",str[i]);
    } else {
        printf ("character %c is not a digit\n",str[i]);
    }
    i++;
}

Remember to click the grey check mark (the ✓) to the left of my answer if it answers your question or led you to the answer!

Upvotes: 0

Sarp Kaya
Sarp Kaya

Reputation: 3784

One way is to use string instead of char, because string has a length function

so like this:

std::string dblNumOne;
std::cout<<"Enter your first number: ";
std::cin >> dblNumOne;
std::cout << dblNumOne.length() << endl;

If you have to use char, then you can simply do it by checking '\0' value which stands for end of string.

My approach would be use the string and get the length of it and then convert it to char array by using c_str() function.

Upvotes: 0

Chris O
Chris O

Reputation: 5037

Simple, just iterate through the std::string as such:

std::string dblNumOne;
std::cin >> dblNumOne;

for(unsigned int i = 0; i < dblNumOne.length(); i++)
{
    if (isalpha(dblNumOne[i]))
    {
        printf ("character %c is alphabetic\n", dblNumOne[i]);
    }
    else
    {
        printf ("character %c is not alphabetic\n", dblNumOne[i]);
    }
}

Upvotes: 2

Related Questions