The task is very simple: the input receives the number of received lines, and then the lines themselves. The output should be (through the gap) the number of vowels in these lines.

// Подсчет гласных в строке #include <iostream> #include <string> #include <algorithm> #include <conio.h> using namespace std; class count_predicate { public: bool operator()(const char& expr) { if ( expr == 'a' || expr == 'o' || expr == 'i' || expr == 'u' || expr == 'y' || expr == 'e' ) return true; return false; } }; int main() { int N; // Нужное количество строк cin >> N; string strs[N]; for (int i = 0; i < N; i++) getline(cin, strs[i]); for (int i = 0; i < N; i++) { int n = count_if(strs[i].begin(), strs[i].end(), count_predicate()); cout << n << " "; } return 0; } 

When instead of getline() I use the usual cin>> , then everything works, but the lines come across with spaces, so vowels are considered incorrect. Here, for some reason, getline() somehow "interrupts" cin from above, and reads the number of lines as another line, as I understood from the program test (but this is not accurate). Actually, why is this happening?

And yet, I have a feeling that I am solving a problem very clumsy, is there any more elegant solution?
I am especially confused by the number of checks in the predicate. If there is such a solution, it is possible without STL, I use it exclusively for educational purposes, consolidate, so to speak, the material that has been studied.

  • It is not at all clear what is happening with getline. Could you demonstrate what you type using getline, what you expect to receive, and what you get as a result. - Vlad from Moscow
  • Using getline, I read the lines to then count the vowels in them. First, I enter the required number of lines in cin, and then in the first cycle I enter these lines themselves. For example: 2 (number of input lines) aghqvi yioqiua press Enter and here I have to enter the second line, but I get the output: 0 8 - niki4iko
  • I already understood and wrote you an answer to your question. - Vlad from Moscow

2 answers 2

Here, for some reason, getline () somehow "interrupts" cin from above, and reads the number of lines as another line, as I understood from the test program (but this is not accurate)

No, everything is easier. After reading the number of lines in the buffer, there is a newline character. You just need to flush the buffer after reading N - for example, like this:

 cin.ignore(std::numeric_limits<size_t>::max(), '\n'); 

(this means - read and ignore everything until we meet '\ n').

Also - I would rewrite your operator as

 bool operator()(char expr) { return strchr("aoiuye",expr) != nullptr; } 

The rest did not look ...

  • Your decision helped, thanks. But, if not hard, could you describe in a bit more detail how cin.ignore works? For example, in the std :: numeric_limits <> parameter I entered instead of <size_t> <char> and everything works for me anyway. - niki4iko
  • one
    It works because you do not enter, say, 300 characters after N :) This function reads no more than the first character parameter and throws them out, but if on the way it encounters a character specified as the second parameter, it discards it and stops reading. Those. Russian language - "count and throw everything to the end of the line, but no more than ..." There you can even put a prime number - of type ignore(200,'\n') , but I scored a maximum value of type size_t . Just as for me - it is more correct, or something ... Take a look here - en.cppreference.com/w/cpp/io/basic_istream/ignore - Harry

After this sentence

 cin >> N; 

The newline character '\n'. Поэтому первое обращение к буферу ввода с помощью функции has been saved in the input stream buffer '\n'. Поэтому первое обращение к буферу ввода с помощью функции '\n'. Поэтому первое обращение к буферу ввода с помощью функции getline '\n'. Поэтому первое обращение к буферу ввода с помощью функции leads to an empty line being read.

This newline character should be removed from the buffer before using getline This is done as follows.

 #include <iostream> #include <string> #include <algorithm> #include <limits> ^^^^^^^^^^^^^^^^^ #include <conio.h> using namespace std; class count_predicate { public: bool operator()(const char& expr) { if ( expr == 'a' || expr == 'o' || expr == 'i' || expr == 'u' || expr == 'y' || expr == 'e' ) return true; return false; } }; int main() { int N; // Нужное количество строк cin >> N; string strs[N]; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // ... 

Predicate can be written easier

 #include <cstring> //... struct count_predicate { bool operator()( const char &c ) const { const char *vowels = "aeiouy"; return std::strchr( vowels, c ); } };