Recalcitrant isspace()

I loaded this branching demo sketch from a C++ book I've got and made some minor alterations to run it on an UNO. Entering from the serial monitor it correctly counts letters, digits, and punctuation but will not count spaces. For instance, entering "[color=blue]11  !!  aa  ,,[/color]" displays 2 letters, 0 whitespace, 2 digits, 4 punctuation, 0 other.

The two #includes didn't make any difference so they're commented out. It also doesn't make any difference whether the function names have capital letters embedded - isdigit() works identically to isDigit().

I also tried a cout statement in the isspace() section and it never printed. So the space character never passes the 'is it a space' test.

I'm using #include <ArduinoSTL.h> to reduce the amount of modification needed to run the demo programs on the Arduino - mainly eliminating the getting-and-displaying-data translation tedium.

// name   cctypes.cpp --

// C++ primer page 271

//#include <PrintStream.h>
#include <ArduinoSTL.h>
//#include <ctype.h>              // prototypes for character functions

char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  std::cout << "Enter text for analysis, and type @"
            " to terminate input.\n";
  std::cin >> ch;              // get first character
  while (ch != '@')            // test for sentinel
  {
    if (isAlpha(ch))        // is it an alphabetic character?
      chars++;
    else if (isspace(ch))   // is it a whitespace character?
      whitespace++; // space counter doesn't work!?
    else if (isdigit(ch))   // is it a digit?
      digits++;
    else if (ispunct(ch))   // is it punctuation?
      punct++;
    else
      others++;
    std::cin >> ch;          // get next character
  }
  std::cout << chars << " letters, \n"
            << whitespace << " whitespace, \n"
            << digits << " digits, \n"
            << punct << " punctuations, \n"
            << others << " others.\n";
  return 0;
}

Why is isspace() not working? Possibly a quirk of the serial monitor? Or the library?

The problem is not isspace(), it's std::cin. It has "trouble" with whitespaces.

It may be a little coarse, but try this:

(I switched to 9600 baud for testing...)

// name   cctypes.cpp --

// C++ primer page 271

//#include <PrintStream.h>
#include <ArduinoSTL.h>
//#include <ctype.h>              // prototypes for character functions

char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
char input[20];

void setup() {
  Serial.begin(9600);
  while(!Serial);
}

void loop() 
{
    int i;
        
  std::cout << "Enter text for analysis, and type @ to terminate input.\n";
  std::cin.getline(input,sizeof(input));
  i=0;
  do            // test for sentinel
  {
    ch = input[i++];
    if( isAlpha(ch) )
        chars++;
    else if (isSpace(ch))   // is it a whitespace character?
      whitespace++; // space counter doesn't work!?
    else if (isDigit(ch))   // is it a digit?
      digits++;
    else if (isPunct(ch))   // is it punctuation?
      punct++;
    else
      others++;
       
  }while (ch != '@');
  
  std::cout << chars << " letters, \n"
            << whitespace << " whitespace, \n"
            << digits << " digits, \n"
            << punct << " punctuations, \n"
            << others << " others.\n";
  
  while(1);
}

Blackfin:
The problem is not isspace(), it's std::cin. It has "trouble" with whitespaces.

It may be a little coarse, but try this:

Coarse is fine if it works, and your fix does. Thank you! karma++

Is there any way to get std::cin to behave or is this just its nature?