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