Em đang gặp lỗi lặp vô hạn khi nhập sai giá trị. Giờ em muốn chương trình hiểu là nhập sai và yêu cầu nhập lại . Mọi người có thể chỉ cho em sửa như thế nào được không ạ?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class CongNhan {
private:
string ten;
int sanphamNgay;
string gioitinh;
public:
CongNhan(string ten = "", int sanphamNgay = 0, string gioitinh = "") {
this->ten = ten;
this->sanphamNgay = sanphamNgay;
this->gioitinh = gioitinh;
}
string getTen() {
return ten;
}
string getGioiTinh() {
return gioitinh;
}
int getSanPhamNgay() {
return sanphamNgay;
}
void setSanPhamNgay(int sp) {
sanphamNgay = sp;
}
};
class QuanLy {
private:
CongNhan *congnhan;
int soCongNhan;
int soNgayCong;
public:
QuanLy(int soCongNhan = 0, int soNgayCong = 0) {
this->soCongNhan = soCongNhan;
this->soNgayCong = soNgayCong;
congnhan = new CongNhan[soCongNhan];
}
void NhapThongTin() {
for (int i = 0; i < soCongNhan; i++) {
string ten;
int sanphamNgay;
string gioitinh;
cout << "Nhap ten cua cong nhan thu " << i + 1 << ": ";
cin.ignore();
getline(cin, ten);
cout << "Gioi tinh cua cong nhan thu " << i + 1 << ": ";
cin >> gioitinh;
cout << "Nhap so san pham/ngay cua cong nhan: ";
cin >> sanphamNgay;
congnhan[i] = CongNhan(ten, sanphamNgay, gioitinh);
}
}
void ToanBoThongTin() {
for (int i = 0; i < soCongNhan; i++) {
cout << "Cong nhan thu " << i + 1 << ": " << endl;
cout << "Ten: " << congnhan[i].getTen() << endl;
cout << "Gioi tinh: " << congnhan[i].getGioiTinh() << endl;
cout << "San pham/ngay: " << congnhan[i].getSanPhamNgay() << endl;
cout << "-------------------------" << endl;
}
}
void TimKiemThongTin(string ten) {
bool timkiem = false;
for (int i = 0; i < soCongNhan; i++) {
if (congnhan[i].getTen() == ten) {
cout << "Ten cong nhan: " << congnhan[i].getTen() << endl;
cout << "Gioi tinh cong nhan: " << congnhan[i].getGioiTinh() << endl;
cout << "San pham/ngay: " << congnhan[i].getSanPhamNgay() << endl;
cout << "-------------------------" << endl;
timkiem = true;
break;
}
}
if (!timkiem) {
cout << "Khong tim thay cong nhan co ten " << ten << endl;
}
}
void ThongKe() {
int tongSanPham = 0;
for (int i = 0; i < soCongNhan; i++)
{
tongSanPham += congnhan[i].getSanPhamNgay();
}
cout << "Tong so san pham cua tat ca cong nhan: " << tongSanPham << endl;
cout << "So ngay lam viec: " << soNgayCong << endl;
cout << "Nang suat trung binh cua doi la: " << (float)tongSanPham / soNgayCong << " san pham/ngay" << endl;
}
void Thidua() {
for (int i = 0; i < soCongNhan; i++) {
int nangSuat = congnhan[i].getSanPhamNgay();
string xepLoai;
if (nangSuat < 4) {
xepLoai = "Khong Dat";
} else if (nangSuat <= 7) {
xepLoai = "Dat";
} else if (nangSuat <= 10) {
xepLoai = "Tot";
} else {
xepLoai = "Khong thay";
}
cout << setw(10) << congnhan[i].getTen() << " : Nang suat lao dong: " << nangSuat << " =>> " << xepLoai << endl;
}
}
void NangSuatLaoDongMax() {
int max = 0;
CongNhan *cnMax = nullptr;
for (int i = 0; i < soCongNhan; i++) {
if (congnhan[i].getSanPhamNgay() > max) {
max = congnhan[i].getSanPhamNgay();
cnMax = &congnhan[i];
}
}
if (cnMax != nullptr) {
cout << "Cong nhan co nang suat lao dong cao nhat la: " << endl;
cout << "Ten: " << cnMax->getTen() << endl;
cout << "Gioi tinh: " << cnMax->getGioiTinh() << endl;
cout << "Nang suat lao dong: " << max << endl;
} else {
cout << "Khong tim thay cong nhan nao." << endl;
}
}
};
class DanhSachQuanLy {
private:
QuanLy *danhSach;
int soDoiCongNhan;
public:
DanhSachQuanLy(int soDoiCongNhan = 0) {
this->soDoiCongNhan = soDoiCongNhan;
danhSach = new QuanLy[soDoiCongNhan];
}
~DanhSachQuanLy() {
delete[] danhSach;
}
void NhapThongTin() {
for (int i = 0; i < soDoiCongNhan; i++) {
int soCongNhan, soNgayCong;
cout << "Nhap so cong nhan cho doi cong nhan thu " << i + 1 << ": ";
cin >> soCongNhan;
cout << "Nhap so ngay cong cho doi cong nhan thu " << i + 1 << ": ";
cin >> soNgayCong;
danhSach[i] = QuanLy(soCongNhan, soNgayCong);
danhSach[i].NhapThongTin();
}
}
void ToanBoThongTin() {
for (int i = 0; i < soDoiCongNhan; ++i) {
cout << "Doi cong nhan thu " << i + 1 << endl;
danhSach[i].ToanBoThongTin();
}
}
void TimKiemThongTin(string ten) {
for (int i = 0; i < soDoiCongNhan; ++i) {
cout << "Doi cong nhan thu " << i + 1 << endl;
danhSach[i].TimKiemThongTin(ten);
}
}
void ThongKe() {
for (int i = 0; i < soDoiCongNhan; ++i) {
cout << "Doi cong nhan thu " << i + 1 << endl;
danhSach[i].ThongKe();
}
}
void Thidua() {
for (int i = 0; i < soDoiCongNhan; ++i) {
cout << "Doi cong nhan thu " << i + 1 << endl;
danhSach[i].Thidua();
}
}
void NangSuatLaoDongMax() {
for (int i = 0; i < soDoiCongNhan; ++i) {
cout << "Doi cong nhan thu " << i + 1 << endl;
danhSach[i].NangSuatLaoDongMax();
}
}
};
int main() {
int soDoiCongNhan;
cout << "Nhap so doi cong nhan: ";
cin >> soDoiCongNhan;
DanhSachQuanLy dsQuanLy(soDoiCongNhan);
cout << "Nhap thong tin cho cac doi cong nhan" << endl;
dsQuanLy.NhapThongTin();
int chon;
string ten;
do {
cout << "=========================" << endl;
cout << "1. Xem toan bo thong tin" << endl;
cout << "2. Tim kiem thong tin" << endl;
cout << "3. Thong ke" << endl;
cout << "4. Danh gia nang suat lao dong tung cong nhan" << endl;
cout << "5. Tim cong nhan co nang suat lao dong cao nhat" << endl;
cout << "6. Thoat" << endl;
cout << "=========================" << endl;
cout << "Nhap lua chon cua ban: ";
cin >> chon;
switch (chon) {
case 1:
dsQuanLy.ToanBoThongTin();
break;
case 2:
cout << "Nhap ten cong nhan can tim: ";
cin.ignore();
getline(cin, ten);
dsQuanLy.TimKiemThongTin(ten);
break;
case 3:
dsQuanLy.ThongKe();
break;
case 4:
dsQuanLy.Thidua();
break;
case 5:
dsQuanLy.NangSuatLaoDongMax();
break;
case 6:
cout << "Cam on quy khach da su dung chuong trinh cua chung toi!" << endl;
break;
default:
cout << "Lua chon cua ban khong hop le. Vui long chon lai." << endl;
}
} while (chon != 6);
return 0;
}