0

how to exit program by pressing Enter key

// C++ program to count occurrences of a given // character #include <iostream> #include <string> using namespace std; // Function that return count of the given // character in the string int countLetterInString(string phase , char c) { // Count variable int cnt = 0; for (int i=0;i<phase.length();i++) // checking character in string { if (phase[i] == c) cnt++;} return cnt; } // Driver code int main() { char c; cout <<"This program counts number of specific characters in phrase."<<endl; string str; cout <<"Enter the phrase or hit Enter key to stop: "; while(getline(cin, str)){ if( ??? ) { break; } else { cout << "Enter character to count in phrase: "; cin>>c; cout <<"There are "<< countLetterInString(str, c)<<" "<<c<<" in the phrase" << endl; } } return 0; } can you guys help me which is need to be written in (???) so that when entering Enter key it will exit the program .

22nd Nov 2017, 7:40 PM
Anh Lam
Anh Lam - avatar
1 ответ
+ 3
This was a quick test result from Code Playground: if((short)(char)str[0] == 0) break; or alternatively, if(str.length < 1) break; Outside Code Playground (in your PC) I don't know if that's the same case, just give it a try :) Hth, cmiiw
22nd Nov 2017, 8:32 PM
Ipang