C++ Standard Input Read till Enter pressed

Description

C++ Standard Input Read till Enter pressed

#include <iostream>
using namespace std;
#include <conio.h>            //for getche()
int main()//from  w w w .j  a v  a  2 s.co  m
{
   int chcount=0;             //counts non-space characters
   int wdcount=1;             //counts spaces between words
   char ch = 'a';             //ensure it isn't '\r'
   cout << "Enter a phrase: ";
   while( ch != '\r' )        //loop until Enter typed
   {
      ch = getche();          //read one character
      if( ch==' ' )           //if it's a space
         wdcount++;              //count a word
      else
         chcount++;              //count a character
   }
   cout << "\nWords=" << wdcount << endl << "Letters=" << (chcount-1) << endl;
   return 0;
}



PreviousNext

Related