This is a program that counts how many letters and numbers a string has,but when I Press Enter to exit after entering,it has no response.
#include <iostream>
using namespace std;
int main()
{
char c;
int nums=0,chars=0;
while(cin>>c){
if(c>='0'&&c<='9'){
nums++;
}else if((c>='A'&&c<='Z')||(c>='a'&&c<='z')){
chars++;
}
}
printf("nums:%d\nchars:%d",nums,chars);
return 0;
}
std::isdigit
. – Anoop Ranastd::cin
, it just ends a line. Try usingstd::getline
and walking across astd::string
. – jkbbreak;
. Better way is to get line and ditch the while as jkb said... – AbelCtrl+D
on Linux/Mac, andCtrl-Z
on Windows would send EOF and break the loop. If you redirect input from a file it will also exit when the end of the file is reached. – Retired Ninja