Hello, I need to test an array of chars to see if they are numbers. I am trying isdigit() but that is not working. Does anyone know how I can do this?

Here is the code, it is in three seperate files. I try to test the char in the testArry file, under int testArray::digits

Main:

#include <iostream>
#include "testArray.cpp"

using namespace std;

int
main()
{
 //Array arr;
 testArray test;
 char ch;
 
 cout << "Enter a sentence:" << endl;

 ch = cin.get();
 
 while(ch != '\n')
 {
 test.store_char(ch);
 ch = cin.get();
 }

 cout << "Number of vowels: " << test.vowels() << endl;
 
 cout << "Number of digits: " << test.digits() << endl;
 
 system("pause");
}

array.cpp:
#include <iostream>

using namespace std;

class Array
{
 protected:
          char word[50];
          int size;
          int i;
          char theWord;
 public:
        Array();
        void store_char(char);
        char return_char(int);   
        int return_size(void);
};

Array::Array()
{
 int i;
 for(i = 0; i < 50; i++)
 word[i] = 0;             
}

void
Array::store_char(char userEnter)
{
 size++;
 word[i] = userEnter;
 i++;         
}     

char
Array::return_char(int num)
{
         theWord = word[num];
         return theWord;
}

int
Array::return_size()
{
 return size;
}

testArray.cpp:
#include <iostream>
#include "Array.cpp"
using namespace std;

class testArray:public Array
{
 protected:
          int numVowels;
          int numDigits;
          int numOther;
          char wordEntered;
          char numEntered;
 public:
        int vowels();;
        int digits();
        int spaces();
        int other();
        char backwards();  
};

int
testArray::vowels()
{
  numVowels = 0;

  for(int i = 0; i < return_size(); i++)
  {
       wordEntered = tolower(return_char(i)); 
       switch(wordEntered)
       {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            numVowels++;
            break;
        default:
                numOther++;
       }
  }
       return numVowels;
               
}

int
testArray::digits()
{
 for(int i = 0; i < return_size(); i++)
 {
  numEntered = tolower(return_char(i));
  cout << numEntered << endl;
  if(isdigit(numEntered))
  {   
      numDigits++;
      numOther--;
  }
 } 
 return numDigits;           
}

int
testArray::spaces()
{
 numOther--;
}

int 
testArray::other()
{
 return numOther;                 
}

char
testArray::backwards()
{
                     
}

never mind I figured it out.

Please post the solution.

The solution was just in the function int testArray::digits() setting numDigits = 0 on the first line of that function.

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.