Where can there be a mistake?

The problem is in the withdrawal of the phone by the name of the subscriber. Namely, the comparison of the user's request with ms [i] .fio does not work.

#include<stdio.h> #include<string.h> #include<conio.h> //для getch() #include<clocale> //для локализации int main() { setlocale(LC_CTYPE, "Russian"); char familiya[15]; struct ABONENT { char fio[21]; int tel; int god; }; FILE *file; struct ABONENT ms[300]; int i = 0; //очередной элемент массива int j; // номер символа фамилиии if ((file = fopen("абоненты.txt", "r")) == NULL) puts("В текущем каталоге файл не найден"); // чтение информации из файла в массив ms* while (!feof(file)) { fgets(ms[i].fio, 15, file); fscanf(file, "%d %d", &ms[i].tel, &ms[i].god); i++; } // поиск по фамилиии puts("Пожалуйста, введите фамилию абонента"); int k = i; // номер последнего элемента массива ms for (i = 0; i <= k; i++) { for (j = 0; j <= 21; j++) { if (ms[i].fio[j] == ' ') ms[i].fio[j] = '\0'; } } gets(familiya); bool net = 0; for (i = 0; i <= k; i++) { if (ms[i].fio == familiya) printf("%d", ms[i].tel); net = 1; } if (net == 1) puts("В базе нет абонента с такой фамилией"); getch(); } 

When debugging, it seems that familiya and str [i] .fio match:

alt text

But the result:

alt text

  • indents! My eyes are bleeding from this - Vladimir Gordeev

2 answers 2

 for(i=0;i<=k;i++){ if(ms[i].fio==familiya) printf("%d", ms[i].tel); net=1;} 

Is there a mistake here? try this

 for(i=0;i<=k;i++){ if(ms[i].fio==familiya) printf("%d", ms[i].tel) else net=1;} 
  • oh, beat me a little while i updated the answer :) - Artem
  • Thank you very much) But the main problem was that the lack of a telephone always displays. Probably arrays and strings cannot be compared simply with each other, but only by their elements? - AlexC
  • 2
    @AlexC, well done! You yourself have guessed that it is necessary to compare the elements. In the meantime, you are comparing the addresses of the first elements of 2 arrays (of course, they are not equal). In your case, it is convenient to use the strcmp () function for comparison. How to use read yourself (man 3 strcmp). - avp
  • @avp, Thanks for reminding me about strcmp (). Everything works with him and takes up much less space. - AlexC
  • I'm just wondering, @AlexC and both authors of the "answers" understand what the fundamental error in this code is? if (ms [i] .fio == familiya) - avp
 for(i=0;i<=k;i++){ if(ms[i].fio==familiya) printf("%d", ms[i].tel); net=1;} if(net==1) 

I quietly aside laugh.

 if(ms[i].fio==familiya) //значит нашли фамилию и присвоили переменной net=1; 

and then you write

 if(net==1) puts("В базе нет абонента с такой фамилией"); // а стоит написать "В базе есть абонент!" getch(); 

My little advice.

Check what you write and what conditions you make, so that later there are no stupid mistakes, and not programmatically, but just your own - they are ALWAYS more difficult to find. and the code works for you!

// upd and yes you have the variable net is always 1 :) either add if else or rewrite the code a bit, but probably add if else to this piece of code

  if(ms[i].fio==familiya) printf("%d", ms[i].tel); else net=1; 
  • Many thanks, there will be 15 respects, be sure to like =) - AlexC