Using a do-while loop to count characters - C++ Statement

C++ examples for Statement:do while

Description

Using a do-while loop to count characters

Demo Code

#include <iostream>

int main()/*w  w  w .ja  v a 2  s  .  c  om*/
{
  long count {};
  char ch {};

  std::cout << "Please enter a sequence of characters terminated by '#':" << std::endl;

  do {
    std::cin >> ch;
    ++count;
  } while (ch != '#');

  // do not count '#' as a character
  --count;
  std::cout << "You entered characters (not counting spaces and the terminal #)." << std::endl;
}

Result


Related Tutorials