Чтение информации из файла, добавление элемента в структуру

Суть: чтение из файла помимо массива выводит набор из рандомных символов; добавление элемента в структуру пропускает ввод профессии, со стороны выглядит как даблклик энтера Проблемные участки кода закомментировала. Заранее спасибо за помощь!

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <windows.h>
#include <fstream>
using namespace std;
void showAll();
void addRecord();
void dellRecord();
void sortByName();
void sortByResult();
void showIt();
int num = 8; int capacity = 9;
struct hospital {
    char emloyes[255];
    char department[255];
    char highness[100];
    int amount;
}Hdoc, zamdoc, doc, nur, san, econ, acc, cadr;
hospital ar[8] = {
    {"chief physician", "all dep", "main", 1},
    {"vice-chief phys", "all dep", "main", 2},
    {"doctors", "all doc-type dep", "medium", 12},
    {"nurses", "all doc-type dep", "medium", 25},
    {"sanitares", "all doc-type dep, maintenance", "low", 40},
    {"economs", "economical dep", "medium", 5},
    {"accountants", "economy dep", "main/medium", 5},
    {"other", "HR dep, maintenance, etc", "medium/low", 10}
};
void addInFileString(FILE* f, const char* s);
void ShowProtocol(const char* s);
void printArrToFile(hospital* ar, int& num, FILE* file);
void readFromFile(hospital* ar, int& num, FILE* file);
void addInFile(FILE* f);
int main()
{
    SetConsoleTitleA("Hospital");
    const char* s = nullptr;
    int size = 1;
    FILE* f;
    std::ifstream fi;
    while (true) {
        int n;
        cout << "Choose one option below: " << endl;
        cout << "1. Hospital structure" << endl;
        cout << "2. Add record" << endl;
        cout << "3. Delete record" << endl;
        cout << "4. Sort by profession" << endl;
        cout << "5. Sort by amount" << endl;
        cout << "6. Find the department with less amount of employes" << endl;
        cout << "7. Print array to file" << endl;
        cout << "8. Read from file" << endl;
        cout << "9. Add to file" << endl;
        cout << "10. Exit" << endl;
        cout << "Enter variant: ";
        cin >> n;
        switch (n)
        {
        case 1:
            showAll();
            break;
        case 2:
            addRecord();
            break;
        case 3:
            dellRecord();
            break;
        case 4:
            sortByName();
            break;
        case 5:
            sortByResult();
            break;
        case 6:
            showIt();
            break;
        case 7:
            f = fopen("print.txt", "w");
            printArrToFile(ar, num, f);
            fclose(f);
            break;
        case 8:
            f = fopen("print.txt", "r");
            readFromFile(ar, num, f);
            fclose(f);
            showAll();
        case 9:
            f = fopen("print.txt", "a");
            addInFile(f);
            fclose(f);
            break;
        case 10:
            exit(1);
            break;
        }
    }
    return 0;
}
int getInt(string prompt, int min, int max) {
    int num = 0; string s;
    do {
        cout << prompt;
        getline(cin, s);
        num = atoi(s.c_str());
    } while (num < min || num > max);
    num = atoi(s.c_str());
    return num;
}
char* getStr(string prompt, int maxMemory) {
    cout << prompt;
    string s;
    getline(cin, s);
    if (s.length() >= maxMemory)
        s = s.substr(0, maxMemory - 1);
    //allocate memory for char array
    char* cStr = new char[s.length() + 1];
    return strcpy(cStr, s.c_str());
}
void addInFileString(FILE* f, const char* s)
{
    fprintf(f, "%s\n", s);
}
void ShowProtocol(const char* s)
{
    FILE* f = fopen("log.txt", "a");
    addInFileString(f, s);
    fclose(f);
}
void printArrToFile(hospital* ar, int& num, FILE* file)
{
    ShowProtocol("Printed Arr to file");
    int i = 0; char j = 0;
    for (; i < num, j<num; i++, j++)
    {
        fprintf(file, "%-30s %-30s %-30s %-10d\n", ar[j].emloyes, ar[j].department, ar[j].highness, ar[i].amount);
    }
}
void readFromFile(hospital* ar, int& num, FILE* file)//выводит массив + "мусор" - 143
{
    ShowProtocol("Arr readed from file");
    num = 0;
    while (fscanf(file, "%*s %*s %*s %*d") != EOF)
        num++;
    ar = new hospital[num];
    fseek(file, 0, SEEK_END);
    char a[255], b[255], c[255];
    int i = 0;
    for (; i < num; i++)
    {
        fscanf(file, "%s %s %s %d", &(a), &(b), &(c), & (ar[i].amount));
        //не дает использовать присваивания ниже, выдает ошибку "Должно быть допустимым левосторонним значением"
        /*ar[i].emloyes = a; - 157
        ar[i].department = b; - 158
        ar[i].highness = c;- 159 */ 
    }
}
void addInFile(FILE* f)
{
    hospital add;
    cout << "Insert proffession: " << endl;
    cin >> add.emloyes;
    cout << "Insert department: " << endl;
    cin >> add.department;
    cout << "Insert highness: " << endl;
    cin >> add.highness;
    cout << "Insert amount: " << endl;
    cin >> add.amount;
    ShowProtocol("String added to file");
    fprintf(f, "%-30s %-30s %-30s %-10d\n", add.emloyes, add.department, add.highness, add.amount);
}
void showAll() {
    cout << "Call fucntion showAll()" << endl;
    cout << "Here:\n";
    printf("%-*s %-*s %-*s %s\n", 20, "Employes: ", 50, "Department:", 40, "Higness: ", "Amount: ");
    int i = 0; char j = 0;
    for (; i < num, j < num; i++, j++) {
        printf("%-*s %-*s %-*s %d \n", 20, ar[j].emloyes, 50, ar[j].department, 40, ar[j].highness, ar[i].amount);
    }
}
void growAr() {
    capacity = capacity + 50;
    hospital* newAr = new hospital[capacity];
    memcpy(newAr, ar, num * sizeof(hospital));
    hospital* oldAr = ar;
    swap(newAr, oldAr);
    delete[] oldAr;
}
void addRecord() {//193
    if (num == capacity)  growAr();
    strcpy(ar[num].emloyes, getStr("Enter profession: ", 255));//пропускает, сразу переходит ко след. значению - 195
    strcpy(ar[num].department, getStr("Enter department: ", 255));//196
    strcpy(ar[num].highness, getStr("Enter higness: ", 255));//197
    ar[num].amount = getInt("Enter amount: ", 0, 50);//198
    num++;
    showAll();
}
void dellRecord() {
    cout << "Enter position: ";
    int pos;
    cin >> pos;
    for (int i = pos; i < num - 1; ++i) {
        ar[i] = ar[i + 1];
    }
    --num;
    showAll();
}
typedef bool Comparator(hospital, hospital);
void sort(Comparator* test) {
    for (int i = 0; i < num - 1; i++)
        for (int j = i + 1; j < num; j++)
            if ((!test(ar[i], ar[j]))) {
                hospital x = ar[i];
                ar[i] = ar[j];
                ar[j] = x;
            }
}
bool testByName(hospital Hdoc, hospital zamdoc)
{
    return strcmp(Hdoc.emloyes, zamdoc.emloyes) <= 0;
}
void sortByName() {
    sort(testByName);
    showAll();
}
bool testByResult(hospital Hdoc, hospital zamdoc) {
    if (Hdoc.amount != zamdoc.amount)
        return Hdoc.amount < zamdoc.amount;
    if (Hdoc.amount != zamdoc.amount)
        return Hdoc.amount < zamdoc.amount;
    return strcmp(Hdoc.emloyes, zamdoc.emloyes) <= 0;
}
void sortByResult() {
    sort(testByResult);
    showAll();
}
void showIt() {
    int ball;
    cout << " Enter the number of peoples in department: ";
    cin >> ball;
    sort(testByResult);
    cout << "Departments with less/equal amount: " << ball << ":\n";
    printf("%-*s %-*s %-*s %s\n", 20, "Employes: ", 50, "Department:", 40, "Higness: ", "Amount: ");
    int i = 0; char j = 0;
    for (; i < num, j < num; i++, j++) {
        if (ar[i].amount <= ball)
            printf("%-*s %-*s %-*s %d \n", 20, ar[j].emloyes, 50, ar[j].department, 40, ar[j].highness, ar[i].amount);
    }
}

Ответы (0 шт):