ok so what im trying to do is to read some text from a file. Put them into an array. And count the number of characters in the text file using pointers to access the array. However, when i compile i get an error for this line check = isalpha('*text'); It says the character constant is too long for its type. Im kinda stuck at this point and not sure how to solve it.

const int SIZE = 500;
char fileName[20];
char text[SIZE];

int main()
{                  
    cout << "Enter filename: " << endl;
    cin >> fileName;  
                     
    fstream infile(fileName, ios::in);
    infile.getline(text, SIZE);

    int charNum = countChar(text);
    cout << "Number of characters: " << charNum << endl;
    
infile.close();
return 0;
}

int countChar(char* text)
{      
    int count1 = 0;
    bool check;
    
    while (*text != '\0')
    {         
      check = isalpha('*text');
      if(check == true)
      count1++;
      text++;
           
    }   
    
    return count1;
          
}

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

It would be, isalpha expects one character.

so correct me if im wrong...i read the text file, then put the characters into an array, and im pretty sure *text in check = isalpha('*text'); refers to a single array element....

ahhh....still cant figure out whats wrong

Try to remove the ' ' around *text

ok i tried that and......it created another error....

and the error is... ?

and the error is... ?

invalid conversion from `char**' to `int'

I think there could be a typing error somewhere in your code. I tried this

#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 500;
char fileName[20];
char text[SIZE];

int countChar(char* text);

int main() {                  
    cout << "Enter filename: " << endl;
    cin >> fileName;        
    fstream infile(fileName, ios::in);
    infile.getline(text, SIZE);
    int charNum = countChar(text);
    cout << "Number of characters: " << charNum << endl;
	infile.close();
	return 0;
}

int countChar(char* text) {      
    int count1 = 0;
    bool check;
    while (*text != '\0') {         
      check = isalpha(*text);
      if(check == true)
      	count1++;
      text++;      
    }   
    return count1; 
}

which to me looks like your code and it works fine. I didn't change anything other than indentation and #includes and countChar prototype added...

this is very weird LOL
i deleted the #include <cstring> and the ' ' around *text

IT WORKED! soooo weird

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.