When compiling, the program does not produce errors, but during startup it shows incorrect results:

alt text

It seems to me that she does not read the salary of workers, but I do not know why.

The problem itself:

Given a file containing information about employees. The file entry structure:
- Personnel Number.
- Name and initials of the employee.
- position.
- salary.

Write a program that: 1. displays a list of employees for a given position; 2. display a list of employees with a minimum salary;

The code itself:

#include<stdio.h> #include<string.h> #include<conio.h> #include<clocale> #include<Windows.h> struct SOTRUDNIK { char fio[21]; char dol[21]; int tn; int zarplata; }; void Dol(int k, SOTRUDNIK * str); void Zap(int k, SOTRUDNIK * str); int main() { int zap; setlocale(LC_CTYPE, "Russian"); int i; FILE *f; struct SOTRUDNIK str[300]; if ((f = fopen("сотрудники.txt", "r")) == NULL) puts("Файл не найден."); for (i = 0; !feof(f); i++) { fgets(str[i].fio, 20, f); fgets(str[i].dol, 20, f); fscanf(f, "%d %d", &str[i].tn, &str[i].zarplata); } int k = i; char c; bool proof = true; for (int i = 0; i <= k; i++) { for (int j = 0; j <= 21; j++) { if (str[i].dol[j] == ' ') str[i].dol[j] = '\0'; } } do { if (proof) { puts("___________________________________________"); puts("Выберите действие:"); puts("1-вывод сотрудников по заданной должности"); puts("2-вывод сотрудников с минимальной зарплатой"); puts("3-для выхода"); puts("___________________________________________"); } c = getchar(); proof = true; switch (c) { case '1': Dol(k, str); break; case '2': Zap(k, str); break; case '3': return 0; fclose(f); break; default: proof = false; break; } } while (c = '!3'); } void Dol(int k, SOTRUDNIK * str) { char dolC[21]; bool naiden = 0; puts("Введите должность:"); scanf("%s", dolC); for (int i = 0; i <= k; i++) if (strcmp(str[i].dol, dolC) == 0) { printf("%d %s %s", str[i].tn, str[i].fio, str[i].zarplata); naiden = 1; } if (naiden == 0) printf ("В базе нет работника с такой должностью"); puts("Для возврата в меню нажмите любую клавишу"); getchar(); getchar(); system("cls"); } void Zap(int k, SOTRUDNIK * str) { int minzap = 100000; puts("Сотрудники с минимальной зарплатой:"); puts("\n № ФИО зарплата"); puts("___________________________________________"); for (int i = 0; i <= k; i++) { if (minzap > str[i].zarplata) minzap = str[i].zarplata; } for (int i = 0; i <= k; i++) if (minzap == str[i].zarplata) printf("\n %d %s %d", str[i].tn, str[i].fio, str[i].zarplata); puts("___________________________________________"); puts("Для возврата в меню нажмите любую клавишу"); getchar(); getchar(); system("cls"); } 

The data from the text file employees.txt:

 1 Васильев секретарь 5000 2 Зубков менеджер 6000 3 Джексон менеджер 4000 

Help, please, this problem has been hindering me for several days, at first I didn’t want to bother with trivia.

  • 2
    when you insert a code with the wrong indents, you reduce the chances of an answer three times. Corrected as it should. - Vladimir Gordeev
  • @Vladimir Gordeev, thanks) - jenya
  • one
    1. In what language do you write a program: in C or C ++? 2. In what order are the data in the file employees.txt? And in the program? If you use sishnye calls, it is better to read the entire line through fgets to the end, and then take sscanf data from it. If you write in C ++, it is better to use iostream. 3. What is while(c='!3'); ? - alexlz

1 answer 1

1) 67 line:

 while (c = '!3') -> while(c != '3'); 

2) In Dol and Zap functions in cycles i<k

3) Data in the wrong order is read from the file. In theory, it is necessary:

 fscanf(f, "%d %s %s %d", &str[i].tn, &str[i].fio,&str[i].dol,&str[i].zarplata); 

4) 78 line: format string is wrong. Necessary: "%d %s %d\n"

5) In the input cycle add

 printf("%d %s %s %d\n",str[i].tn, str[i].fio,str[i].dol,str[i].zarplata); 

and see how the data is entered. It is generally unsafe to use char arrays and Cyrillic. There may be problems with the encodings of the input file and the console.

Ps. And in general, the code is written not so good. As variable names it is better to use English names, rather than transliteration. Also, the magic constants must be replaced by define:

 #define MAX_STR_LEN 21 #define MAX_EMPLOYEES 100 

PS2. Sorry, I can not figure out even with the formatting :)

  • Problems with encodings may well be. Here you can advise after entering the array to display it. If there are kryakozyabry, then the case is the same. - alexlz
  • @ololo: it seems to me that it is better not to give the correct code, but only to indicate where it is wrong and why. All the same task training. - VladD
  • @VladD: yes, probably in vain so immediately painted. But in principle, the author, despite the sloppy style, wrote an almost working program. But some moments really make you think that he just copied it from somewhere and could not figure it out. - ololo
  • @ololo: I also thought that the vehicle somewhere took the finished program and only slightly redid it for his task. In any case, I hope the discussion will help to understand and understand, and finish everything yourself. - VladD
  • @ololo, thanks to your advice figured out =) The most useful tip is to add printf () to the input cycle. - jenya