Good day to all. Here's a question:

cout << "Введите количество строк, которые хотите ввести -> "; cin >> m; for (int i = 0; i < m; i++) { cout << "Введите строку -> "; cin.ignore(); gets_s(str); pptr = ADDstr(pptr, sz, str); sz++; } SHOWstr(pptr, sz); 

The program asks for strings from the user and then displays, after the second iteration of the loop, the first character disappears from the lines, its gets_s () function loses, but I haven’t managed to overcome it yet. Do not tell me how to solve this problem?

  • So you yourself call the cin.ignore() method, which drops the first character from the input stream. - Sergey Gornostaev
  • If cin.ignore () is not called, then gets_s () will be completely ignored. cin.ignore () catches clicking on enter, problem in gets_s () - Exsa_N

1 answer 1

The gets_s function gets_s called with two parameters. But this is unimportant, because, generally speaking, the very wrong approach is to mix C ++ and C. Why don't you use the appropriate getline functions?

And if you really want a mixture of French and Nizhny Novgorod, then cin.ignore() should be called once, immediately after cin >> m; .

  • Unfortunately, the academy has not yet taught us how to use classes like "string", so I will interfere with French and Nizhny Novgorod) Thank you very much! - Exsa_N
  • You watched the link? I specifically gave you a link to read in a regular string buffer! Almost complete analog gets_s ! - Harry
  • Just tried it, this is what I need! cin.ignore() is still needed as far as I understand? The only difference is that cin.getline() is a C ++ function, and gets_s() is a C function, right? I will know, thank you :) - Exsa_N
  • And it also turns out that, in gets_s() you can pass only the name of the array, if it already has a dimension, and with cin.getline() this will not work - Exsa_N
  • Generally, it also gets_s to pass a size in gets_s - see en.cppreference.com/w/c/io/gets As for getline , unlike gets_s it works not only with standard input, but with any stream. ignore needed after cin >> m; to remove unread from input buffer \n - Harry