#include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; #define N 4 struct mystruct { int pp_num; char *fio; char *mark; int groupe_num; }; int main() { mystruct a[N]; a[0].pp_num = 1; a[0].fio = "Yarmolenko AA"; a[0].mark = "4,5,5,3,4"; a[0].groupe_num = 12; a[1].pp_num = 2; a[1].fio = "Pyatov AV"; a[1].mark = "3,4,5,4,3"; a[1].groupe_num = 23; a[2].pp_num = 3; a[2].fio = "Shevchenko AM"; a[2].mark = "4,3,4,2,4"; a[2].groupe_num = 34; a[3].pp_num = 4; a[3].fio = "Garmash DH"; a[3].mark = "3,5,3,4,4"; a[3].groupe_num = 43; for (int i = 0; i < N; i++) { char c[2] = "2"; char *b; b = strpbrk(a[3].mark,c); if (b!=NULL) { cout << a[i].fio << endl; cout << endl; } } _getch(); return 0; } 

should bring out the name Shevcenko but does not work I can not figure it out

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

1) You loop through all the elements of the array, but look for "2" only in the fourth element: strpbrk (a [ 3 ] .mark, c) :-)

2) Why strpbrk() ?

3) Why is c[] created and initialized at each iteration, and generally not a constant?

 const char c[] = "2"; /* ... */ for (size_t i = 0; i < sizeof(a)/sizeof(a[0]); i++) { /* а если ищем только "2", то можно ещё проще: const char c = '2'; ... !strchr(a[i].mark, c) */ if ( !strstr(a[i].mark, c) ) { cout << a[i].fio << "\n\n"; } } 

    To begin with, you should include the <cstring> header, since you are using the functions declared in this header.

     #include <cstring> 

    String literals in C ++ have the type of constant character arrays, that is, const char[N] , where N is the number of characters, including the terminating zero, in the string literal. So pointers to string literals should also have const qualifier, that is, it would be correct to define your structure as

     struct mystruct { int pp_num; const char *fio; const char *mark; int groupe_num; }; 

    In the loop, you always check the same element of the array with index 3

     b = strpbrk(a[3].mark,c); 

    Therefore, in this case, there is little point in using a loop. Moreover, this element does not have a '2' in the mark field

     a[3].pp_num = 4; a[3].fio = "Garmash DH"; a[3].mark = "3,5,3,4,4"; a[3].groupe_num = 43; 

    Therefore, it would be correct to write

     char c[] = "2"; for ( int i = 0; i < N; i++ ) { if ( strpbrk( a[i].mark, c ) != nullptr ) { cout << a[i].fio << endl; cout << endl; } } 

    If only one mark is searched in the mark field, then the strstr or strchr function can also be an alternative to the strpbrk function (since the score can be represented by a single character). The advantage of the strpbrk function is that you can search for any of the given ratings. for example

     char c[] = "23"; ^^^^^^^^^^^^^^^ for ( int i = 0; i < N; i++ ) { if ( strpbrk( a[i].mark, c ) != nullptr ) { cout << a[i].fio << endl; cout << endl; } } 

      In addition to the rest of my 5 cents :)

      Since you have C ++, do not use

       #define N 4 

      Write instead more secure from all angles.

       constexpr int N = 4; 

      or, if the compiler is old and does not understand constexpr ,

       const int N = 4; 

      Also - yes, in your particular case, such a focus

       a[0].fio = "Yarmolenko AA"; 

      it works, but conceptually it’s not worth doing. Because when you remake a program, you may be assigned a string, for example, entered from a keyboard into a buffer. With this assignment, you save only a pointer to a buffer, in which, after the next input, there will be a completely different line. It is better to copy the string to your internal buffer.