Here i have problem with ctype.h library .I think i use correctly,but i ahve an error on it.
This is the code.

#include<iostream>
#include <ctype.h>
using namespace std;
int main(){
	char letter;
	int num;
	int isdigit(int letter);
	int  isupper (int letter);
	int islower( int letter);
	int small=0;
	int capital=0;
	int number=0;
	int special=0;

	 cout<<"Enter any number and letter you like,'+'means   exit";
	 /* use if statement to help user to count how many times s/he enter letter*/
   for(int i=0;i<=5;i++)  //any number greater than one
	{     //start while here
	

	  cin>>letter; //user enter a number
	  if (letter==isdigit(letter))
		  number++;
	  if(letter==isupper(letter))
		capital++;  //counting capital letter
	  else  if(letter==islower(letter))
		  small++;  //in case enter small, letter counting small letter
	  
	  else if(letter!=isupper(letter) && letter!=islower(letter) && num!=isdigit(num))
		  special++;  /*if the user enter a sepcial                                    chraecter*/

   }
	  
	
	

      cout<<number<<endl<<small<<endl<<capital<<endl<<special;
return 0;
}

Dani AI

Generated

The code has a few distinct problems that make the ctype calls behave incorrectly: function prototypes were (re)declared inside main, the is... functions were compared to the character value instead of tested, a loop/sentinel mismatch (your prompt mentions '+' but the loop is fixed-length), and an unused/incorrect num variable. As noted, the is... functions return an int that should be tested, not compared to the character. Prefer the C++ header <cctype> and call the functions in the std:: namespace.

A simple, corrected approach (reads characters until +) — note the cast to unsigned char before calling the is... functions:

#include <iostream>
#include <cctype>

int main() {
    char ch;
    int digits = 0, lower = 0, upper = 0, special = 0;

    std::cout << "Enter characters (enter '+' to finish): ";
    while (std::cin >> ch && ch != '+') {
        unsigned char uch = static_cast<unsigned char>(ch);
        if (std::isdigit(uch)) ++digits;
        else if (std::isupper(uch)) ++upper;
        else if (std::islower(uch)) ++lower;
        else ++special;
    }

    std::cout << digits << '\n' << lower << '\n' << upper << '\n' << special << '\n';
    return 0;
}

Notes and troubleshooting tips:

  • Do not redeclare isdigit, isupper, etc. inside main. They’re provided by the header.
  • Cast to unsigned char (or pass an int in the unsigned char range) because calling std::isdigit with a negative char value is undefined behavior on many platforms.
  • Use if (std::isdigit(...)) rather than == comparisons; std::isdigit returns nonzero for true.
  • If you need to count whitespace (spaces/newlines), use std::cin.get() or read a whole line with std::getline and iterate the string.
  • Remove unused variables like num and use clear names (lower, upper, digits, special).

These fixes address the logic errors and make the counting reliable across inputs and platforms.

Recommended Answers

All 2 Replies

'is' functions return a boolean result ('is' true or isn't true, otherwise known as false).

So

if ( isdigit(letter) ) {
  number++;
}

Oh, and in C++, use #include <cctype>

Thanks so much.

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.