удаление строки из структуры
После ввода пользователем определенной профессии, вся строка связанная с ею должна удалятся. Код:
#pragma warning(disable:4996)
#include <stdio.h>
#include <list>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <windows.h>
using namespace std;
typedef void MenuFunc();
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;
hospital* next;
hospital *head;
hospital*curr = head;
hospital*trail = 0;
}Hdoc, zamdoc, doc, nur, san, econ, acc, cadr;
hospital ar[8] = {
{"chief physician", "all dep", "main", 1},
{"vice-chief phys", "all dep", "main", 1},
{"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}
};
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());
}
struct MenuUnit {
string text;
MenuFunc* func;
};
MenuUnit menu[] = {
{"Hospital structure", showAll},
{"Add record", addRecord},
{"Delete record", dellRecord},
{"Sort by profession", sortByName},
{"Sort by amount", sortByResult},
{"Find the department with less amount of employes", showIt},
{"Exit", NULL}
};
int main()
{
SetConsoleTitleA("Hospital");
while (true) {
int count = 0;
for (; ; count++) {
cout << " " << count + 1 << ". "
<< menu[count].text << endl;
if (menu[count].text.compare("Exit") == 0)
break;
}
int variant = getInt(" Enter variant number, please ", 1, count + 1);
if (variant == count + 1) {
cout << "\n End.\n";
break;
}
menu[variant - 1].func();
}
system("pause");
}
void showAll() {
cout << "Call fucntion showAll()" << endl;
cout << "Here:\n";
printf(" %-*s %-*s %-*s %s\n", 20, "Employes: ", 25, "Department:", 30, "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, 25, ar[j].department, 30, ar[j].highness, ar[i].amount);
}
}
void growAr() {
capacity = capacity + 10;
hospital* newAr = new hospital[capacity];
// Copy old array to new
memcpy(newAr, ar, num * sizeof(hospital));
hospital* oldAr = ar;
swap(newAr, oldAr);
// Old Deleting
delete[] oldAr;
}
void addRecord() {
if (num == capacity) growAr();
strcpy(ar[num].emloyes, getStr("Enter profession: ", 255));
strcpy(ar[num].department, getStr("Enter department ", 255));
strcpy(ar[num].highness, getStr("Enter higness ", 255));
ar[num].amount = getInt("Enter amount ", 0, 50);
num++;
showAll();
}
void delRecord() {
strcpy(ar[num].emloyes, getStr("Enter profession: ", 255));
int i = 0; char j = 0;
for (; i < num, j < num; i++, j++) {
ar[j].emloyes = "";//127
ar[j].department = "";//128
ar[j].highness = "";//129
ar[i].amount = "";//130
}
}
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 doc) {
if (Hdoc.highness != doc.highness)
return Hdoc.highness < doc.highness;
if (Hdoc.amount != doc.amount)
return Hdoc.amount < doc.amount;
return strcmp(Hdoc.emloyes, doc.emloyes) <= 0;
}
void sortByResult() {
sort(testByResult);
showAll();
}
void showIt() {
int ball;
cout << " Enter the number of peoples in department";
cin >> ball;
sort(testByResult);
cout << "\nDepartments with less amount: " << ball << ":\n";
printf(" %-*s %-*s %s %s\n", 30, "Employe", 50, "Department ", 100, "Higness", "Amount");
for (int i = 0; i < num; i++) {
if (ar[i].amount >= ball)
printf(" %-*s %-*s %s %3d\n", 30, ar[i].emloyes, 50, ar[i].department, ar[i].highness, ar[i].amount);
}
}
компилятор жалуется "выражение должно быть допустимым для изменения левосторонним значением" в 127-129 строке и на "значение типа "const char *" нельзя присвоить сущности типа "int" в 130. буду крайне признательна за помощь с кодом!
Ответы (1 шт):
Автор решения: Optimus1
→ Ссылка
struct hospital { char emloyes[255]; char department[255]; char highness[100]; int amount; }
ar[j].emloyes = "";//127
ar[j].department = "";//128
ar[j].highness = "";//129
ar[i].amount = "";//130
-Собствено, если Вам нужно обнулит массив используйт`е memset().
memset(ar[j].emloyes, 0, 254);
Или за место массива char используйт`е std::string и не парьтесь.
std::string emloyes;
emloyes = "";
-Вы пытаетесь присвоить переменной int - некое пустое строковое текстовое значение ?? Переменной в которой хранятся числовые значения - вы хотите присвоить строку?