I've been trying the last few days to get this code to work but it still wont return the exact numbers. Its always atleast like 10 characters off and it probably is a trivial answer but I cant find the solution. I've initialized the charCount to 0 but still it won't run exactly.

int charcount()
{

	int length=1000000;
	int charCount=0;	
	
	FILE * pFile;
	char mychar [1000000];
	char openfile [20];


	cout<<"\nFile name to open: ";
	cin>>openfile;
	pFile = fopen (openfile ,"r");
		
	if (pFile == NULL) perror ("Error opening file");
		 else {
			fgets (mychar , 1000000 , pFile);
		 }

	for ( int i = 0; i < length; i++) {

		if ('a' <= mychar[i] && mychar[i] <= 'z')
	{
		 charCount=mychar[i];
		 charCount++;
	}
}
	puts (mychar);
	cout<<" "<<endl;
	cout<<"Number of Characters: "<<charCount<<endl;
	
	return 0;
}

}

Recommended Answers

All 8 Replies

On line 23, why won't you use isalpha().

On line 23, why won't you use isalpha().

I'm still new at programming, so I'm not quite sure how to use isalpha in my code but I'll try it!

The bound that you use on the loop is not correct. "length" is the total number of characters in your memory buffer, it is not necessarily the number of characters that was read with fgets from the file. C-style strings are terminated by a null character. So, you should add the following to your loop condition:

for ( int i = 0; ((i < length) && (mychar[i] != '\0')); i++) {

This will stop the iteration when the null-character '\0' is found.

Thank you mike for your help, I changed the line in my code but it still came back with the wrong amount of characters again.
The sentence it needs to count within the file is:
hellllloooo i hope this works and i can change and count the letters and characters

but the characters that it comes out with is 116 instead of 83 :s

Well, first of all, there are 69 characters between 'a' and 'z' in that sentence, not 83 (that's counting the spaces too, which are not counted by your loop).

I would try using isalpha() instead. It's in the #include <cctype> header.

OH, and you should get rid of line 25. Why on earth would you do "charCount=mychar;", it doesn't make any sense. Can't believe I missed that the first time around.

The bound that you use on the loop is not correct. "length" is the total number of characters in your memory buffer, it is not necessarily the number of characters that was read with fgets from the file. C-style strings are terminated by a null character. So, you should add the following to your loop condition:

for ( int i = 0; ((i < length) && (mychar[i] != '\0')); i++) {

This will stop the iteration when the null-character '\0' is found.

The loop should be defined as

for ( int i = 0; mychar[i] != '\0'); i++) {

There is no reason at all why length should even be in the test.

Thank you mike for your help, I changed the line in my code but it still came back with the wrong amount of characters again.
The sentence it needs to count within the file is:
hellllloooo i hope this works and i can change and count the letters and characters

but the characters that it comes out with is 116 instead of 83 :s

As Mike pointed out, what's the value of 's' + 1 ?


Might I point out that the information in this post would have given you an answer much sooner since we would have had enough information to analyze the problem.

Also, you need consistent formatting for your code. Please see this. It helps us read your code properly.

Thank you so much for your help! The code works now but it only counts the letters, is there nay way to make it count punctuation or digits?

Thank you as well for giving me the link for the code formatting!

Thank you so much for your help! The code works now but it only counts the letters, is there nay way to make it count punctuation or digits?

Thank you as well for giving me the link for the code formatting!

Of course. What causes it to count only letters? Fix that.

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.