hi im having alot of trouble with this particular code

the idea is to read text from a file and print it on screen, i have done that. Next i needed to ouput the longest word onto the screen, this i have done also

The bit im stuck with: I need to count the number of lower and upper case 'L's in that file and print it on screen (only that letter).

the error i get is: error C2664: 'isalpha' : cannot convert parameter 1 from 'char *' to 'int'

heres what ive got so far:

#include <iostream>
#include <cstring>
//#include <string>
#include <fstream>
#include <cctype>
#include <stdio.h>
using namespace std;

int main()
{
//some predefined variables
	char FileTxt[150];
	char * LWord = " ";
	int iCounter = 0;
	int i = 0;
//--------------------------------------------------------------------------------
//-----------------some more code in between---------------------------
//--------------------------------------------------------------------------------


char *Letter = "l";
char *TokLetter;

	strcpy(TokFile, TokLetter);     //TokFile stores the whole text from a file; im using it for other purposes too, so rather then messing with it, I want to copy it

	while(TokLetter != NULL)
	{
		if(isalpha(Letter))
		{
			iCounter = iCounter + 1;     //increment the int counter everytime it finds an 'L/l' 
		}
		else
		{
		}
		//TokLetter = strtok(NULL, " .1234567890");		
	}

	cout<<"The number of upper and lower case 'l' letters is: "<<iCounter<<endl;
Member Avatar for iamthwee
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

class Foo
{
public:
   int count ( string p )
   {
      int s = p.length();
      int counter = 0;
      for ( int i = 0; i < s; i++ ) {

         if ( tolower ( p[i] ) == 'l' ) {
            counter++;
         }
      }
      return counter;
   }
};

int main()
{
   Foo test;
   cout << test.count ( "What the HelL is that. Lol" );

   cin.get();
}

Or without classes:

int Count(std::string in)
{
    int how_many = 0;
    for (unsigned i = 0; i<in.length(); i++)
        if (in[i] == 'l' || in[i] == 'L')
            how_many++;
    return how_many;
}

int main()
{
    std::cout << Count("What the helL is that?! Lol");
}

Tnx!!! the code works, but not exactly what i need...........i tried tweeking it to suit my situation. I dont know how to use classes yet, so im guessing.

my text i stored in a variable char FileTxt[150] so now i have:

class Foo
{
public:
   int count (char FileTxt[])
   {
	  int s = strlen(FileTxt);
      int counter = 0;
      for ( int i = 0; i < s; i++ ) 
	  {

         if ( tolower ( FileTxt[i] ) == 'l' ) 
		 {
            counter++;
         }
      }
      return counter;
   }
};

//-----------------------------------------------------------

//FileTxt holds the text i need t analise  
	
	Foo test;
	   cout <<"the number of l's in the sentence is: "<< test.count(FileTxt);

	   cin.get();

there are no errors, but the output is wrong............im always getting a 0, so it doesnt actually count anything

Member Avatar for iamthwee
#include <iostream>
#include <string>
#include <ctype.h>

class Foo
{
public:
   int count ( char FileTxt[] )
   {
      int s = strlen ( FileTxt );
      int counter = 0;
      for ( int i = 0; i < s; i++ )
      {
         if ( tolower ( FileTxt[i] ) == 'l' )
         {
            counter++;
         }
      }
      return counter;
   }
}; 

int main()
{
   char FileTxt[100] = "What the helL is that?! Lol";

   Foo test;
   std::cout << "the number of l's in the sentence is: " << test.count ( FileTxt );

   std:: cin.get();
}

I get the right answer with this.

hi im having alot of trouble with this particular code

the idea is to read text from a file and print it on screen, i have done that. Next i needed to ouput the longest word onto the screen, this i have done also

The bit im stuck with: I need to count the number of lower and upper case 'L's in that file and print it on screen (only that letter).

the error i get is: error C2664: 'isalpha' : cannot convert parameter 1 from 'char *' to 'int'

heres what ive got so far:

#include <iostream>
#include <cstring>
//#include <string>
#include <fstream>
#include <cctype>
#include <stdio.h>
using namespace std;

int main()
{
//some predefined variables
	char FileTxt[150];
	char * LWord = " ";
	int iCounter = 0;
	int i = 0;
//--------------------------------------------------------------------------------
//-----------------some more code in between---------------------------
//--------------------------------------------------------------------------------


char *Letter = "l";
char *TokLetter;

	strcpy(TokFile, TokLetter);     //TokFile stores the whole text from a file; im using it for other purposes too, so rather then messing with it, I want to copy it

	while(TokLetter != NULL)
	{
		if(isalpha(Letter))
		{
			iCounter = iCounter + 1;     //increment the int counter everytime it finds an 'L/l' 
		}
		else
		{
		}
		//TokLetter = strtok(NULL, " .1234567890");		
	}

	cout<<"The number of upper and lower case 'l' letters is: "<<iCounter<<endl;

The problem is that isalpha expects an argument of type char (which is an int) but you are passing it a type of char* (pointer to char). Since Letter is a char* (pointer to a char), change isalpha(Letter) to isalpha(*Letter) so it dereferences the pointer to get the value stored at that memory address. I hope that clarifies things for you! ~h3x

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.