Тема: Пошук в масиві структур
Ось код:
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
#include <cstdlib>
#include "windows.h"
#include <string.h>
#include <string>
#include <stack>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
const int nameLength = 41;
const int length_year = 5;
const int saleryLength = 5;
const int bufferLentgh = nameLength + length_year + saleryLength;
struct Woker
{
char name[nameLength + 1];
int birtdayData;
int salery;
};
const int baseLen = 100;
Woker baseW[baseLen];
char buffer[bufferLentgh + 1];
char timeName[nameLength + 1];
ifstream file("db_otd_wokers1.txt");
if (!file.is_open())
{
cout << " Error of opening!!! " << endl;
system("pause");
return 1;
}
int i = 0;
while (!file.eof())
{
file.getline(buffer, bufferLentgh);
strncpy(baseW[i].name, buffer, nameLength);
baseW[i].name[nameLength] = '\0';
baseW[i].birtdayData = atoi(&buffer[nameLength]);
baseW[i].salery = atoi(&buffer[nameLength + length_year+1]);
cout << " Name: " << baseW[i].name << "\t Year: " << baseW[i].birtdayData << " Salery: " << baseW[i].salery << endl;
i++;
if (i > baseLen)
{
puts(" File very big! ");
return 1;
}
}
file.close();
int countOfWokers = i, searchingWokers = 0;
float allSalery = 0;
while (true)
{
cout << " Enter lastname of worker for searching (end if you want exit): ";
cin >> timeName;
OemToChar(timeName, (LPWSTR)timeName);
if (strcmp(timeName, "end") == 0)
{
break;
}
bool not_found = true;
for (int k = 0; k < countOfWokers; k++)
{
if (strstr(baseW[k].name, timeName))
{
if (baseW[k].name[strlen(timeName)] == ' ')
{
cout << " Name: " << baseW[k].name << "\t Year: " << baseW[k].birtdayData << " Salery: " << baseW[k].salery << endl;
searchingWokers++;
allSalery += baseW[k].salery;
not_found = false;
}
}
}
if (not_found)
{
cout << " This woker don`t exist! " << endl;
}
}
if (searchingWokers > 0)
{
cout << " Average of salery = " << allSalery / searchingWokers << endl;
}
system("pause");
return 0;
}
Проблема коду в тому що не коректно зчитується інформація з файлу.
Ось приклад того що містить файл