Hello. I had such a task and I kind of wrote the code, but for some reason it doesn’t print the line I need, or rather it doesn’t print anything at all .... could you point out my mistakes and help me fix it?

#include <iostream> using namespace std; int main() { char digits[] = "1234567890"; char s1[256]; for (int i = 0; i < 2; i++) gets_s(s1); for (int i = 0; i < 2; i++) { for (int j = 0; j < sizeof(s1); j++) { char *s3 = s1; while(!isdigit(*s3)) s3++; int k = strspn(s3,digits); if (k == 2) puts(s1); } } system("pause"); return 0; } 
  • What is the meaning of this mysterious cycle for (int i = 0; i <2; i ++) gets_s (s1) ;? Or do you get paid for the number of entered characters in the program? - Vlad from Moscow
  • Not only does you get the gets_s function incorrectly, because there is no such function in C ++. :) - Vlad from Moscow

1 answer 1

To begin, you should include the <stdio.h> and <cstring> , since you are using functions declared in these headers, such as the strspn function.

This cycle

 for (int i = 0; i < 2; i++) gets_s(s1); 

does not make sense. And besides, it uses an incorrect C function, which by the way is not supported by the C ++ standard, gets_s . This function has two parameters, as seen from its declaration.

 char *gets_s(char *s, rsize_t n); 

you call this function with one argument. Therefore, starting from this point, your program has undefined behavior.

If this function with one parameter is declared as an extension of the language in some compiler, then it is better not to rely on it, since your code in this case will not be portable and will not conform to the C ++ standard.

You should not have this cycle. You should read the line in the next loop. And use standard C ++ functions, such as std::getline , instead of the gets_s function.

This cycle

 for (int j = 0; j < sizeof(s1); j++) 

also incorrect, since the read string may be shorter than the length of the array s1 . Instead of the value of sizeof( s1 ) you should at least use the value returned by the strlen function. Better yet, just focus on the terminating null of the line, as a condition for loop termination.

In this sentence

  if (k == 2) puts(s1); 

You are trying to print a string that contains at least one two-digit number, although you must skip such strings by the condition of the assignment.

When using standard C functions such as strspn and strcspn , the program may look like this

 #include <iostream> #include <cstring> int main() { const size_t N = 256; char s[N]; const char *digits = "0123456789"; while ( std::cin.getline( s, sizeof( s ) ) && s[0] != '\0' ) { const char *p = s; size_t n = 0; do { n = std::strcspn( p, digits ); p += n; n = std::strspn( p, digits ); p += n; } while ( *p && n != 2 ); if ( n != 2 ) std::cout << s << std::endl; } return 0; } 
  • char *gets_s(char *s, rsize_t n); - As I recall, there is another declaration - through a template for arrays of fixed size. - Qwertiy ♦
  • @Qwertiy There is no such function in the C ++ standard. It may be somewhere declared, as an extension of the language of a compiler, but nevertheless, it is better not to rely on it, but to use standard functions. - Vlad from Moscow
  • I did not say anything about the standard. Yes, this is an MSVS extension. But there are two overloads - the one in the answer, and the one in the question. i.stack.imgur.com/BB1Id.png Screenshot, because there is some kind of shamanism with macros in the ad that you __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0( char*, gets_s, char, _Buffer) get: __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0( char*, gets_s, char, _Buffer) . - Qwertiy ♦
  • pastebin.com/7gbcV5Vq - Qwertiy ♦
  • @Roman Not at all. Ask more. :) - Vlad from Moscow