Hàm nhập dữ liệu bị lỗi

Mọi người giải đáp giúp em với ạ, lỗi ở phần void Nhap() nhưng em ko biết sửa thế nào

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<string>
#include<conio.h>
#include <ctype.h>
using namespace std;
struct phongkhachsan{
	int thang[20];
	float giaphong;
	float doanhthu;
	float hieuqua;
	float tongsongaysudung;
	string tenphong;
	string maphong;
};
void nhapsophong(int &n){
	cout<<"Nhap vao so phong : ";
	cin>>n;
}
void nhapthongtin(phongkhachsan &ks){

	fflush(stdin);
	cout<<"Nhap vao ten phong : ";
	getline(cin,ks.tenphong);
	fflush(stdin);
	cout<<"Nhap vao ma phong : ";
	getline(cin,ks.maphong);
	fflush(stdin);
	cout<<"Nhap vao gia phong :";
	cin>>ks.giaphong;
	for(int i=0;i<12;i++){
		cout<<"Nhap vao so ngay phong duoc su dung vao thang "<<i+1<<" : ";
		cin>>ks.thang[i];
	}
	for (int i=0; i<12;i++){
		ks.tongsongaysudung+=ks.thang[i];
	}
	ks.doanhthu=ks.tongsongaysudung*ks.giaphong;
	ks.hieuqua=(ks.tongsongaysudung/365)*100;
}
void nhapdsphong(phongkhachsan DS[],int n){
	for(int i=0; i<n;i++){
		cout<<"Nhap thong tin phong thu "<<i+1<<" : \n";
		nhapthongtin(DS[i]);
	}
}
void xuatphong(phongkhachsan DS[],int n){
	for(int i=0; i<n;i++){
		cout<<"\nPhong thu "<<i+1<<endl;
		cout<<"\nTen phong :" <<DS[i].tenphong;
		cout<<"\nMa phong :"<<DS[i].maphong;
		cout<<"\nGia phong : "<<DS[i].giaphong;
		cout<<"\nSo ngay duoc su dung trong tung thang \n";
		cout<<setw(8)<<"Thang:";
		for(int a=1;a<13;a++){
			cout<<setw(4)<<a;	
		}
		cout<<"\n";
		cout<<setw(8)<<"NSD:";
		for(int j=0;j<12;j++){
			cout<<setw(4)<<DS[i].thang[j];	
		}
				cout<<"\n";

	}	
}
void nhapfile(phongkhachsan ks[],int &n){
	fstream fileInput("danhsachphong.txt");
	if (!fileInput.is_open()){
		cout<<"Khong mo duoc file"<<endl;
		return;
	}
	int K=12;
	int N=0;
	while(!fileInput.eof()){
		getline(fileInput,ks[N].tenphong);
		if(ks[N].tenphong=="")
			break;
		fileInput>>ks[N].maphong;
		if(ks[N].maphong=="")
			break;
		fileInput>>ks[N].giaphong;		
		for(int i =0;i<K;i++)
			fileInput>>ks[N].thang[i];
	N++;
		fileInput.ignore();
	}
	fileInput.close();
	for (int j=0;j<N;j++){
		for (int i=0; i<12;i++){
		ks[j].tongsongaysudung+=ks[j].thang[i];
		}
		ks[j].doanhthu=ks[j].tongsongaysudung*ks[j].giaphong;
		ks[j].hieuqua = (ks[j].tongsongaysudung/365)*100;
	}
	n=N;
}
void xuatfile(phongkhachsan ks[],int N){
		fstream fileOutput("Danhsachphongkhachsan.txt");
	fileOutput<<setw(5)<<"Phong"<<"|"<<setw(10)<<"Gia"<<"|"<<setw(36)<<" So ngay su dung trong thang(ngay)";
	fileOutput<<setw(19)<<"Doanh thu(trVND)"<<" | "<<setw(8)<<"Hieu qua(%)"<<endl;
	fileOutput<<setw(5)<<"_____"<<"|"<<setw(10)<<"__________"<<"|";

	for (int i=1;i<=12;i++){
		fileOutput<<setw(3)<<i;
	}
	fileOutput<<endl;
	for( int i=0; i<N;i++){
		fileOutput<<setw(5)<<ks[i].tenphong<<"|"<<setw(10)<<ks[i].giaphong<<"|";
		for (int j=0;j<12;j++){
			fileOutput<<setw(3)<<ks[i].thang[j];
		}
		fileOutput<<setw(19)<<ks[i].doanhthu/1000000<<" | "<<setw(8)<<ks[i].hieuqua<<endl;
		fileOutput<<endl;
	}
}
void displaymenu(){
cout<<"==============================================="<<"\n";
cout<<"		   MENU				   "<<"\n";
cout<<"_______________________________________________"<<"\n";
cout<<"1.Nhap"<<"\n";	
cout<<"2.Xem "<<"\n";
cout<<"3.Sua"<<"\n";
cout<<"4.Tim kiem"<<"\n";
cout<<"5.Thong ke"<<"\n6.Thoat \n";
}
void Thongke(phongkhachsan DS[],int n){	
	char gao='r';
	while(gao=='r'||gao=='R'){
		cout<<endl;
		cout<<"1.Gia phong \n2.So ngay duoc su dung \n3.Doanh thu\n4.Hieu qua \n" ;
		cout<<"5.Xem chi tiet\n6.Tro lai \n";
		cout<<"Nhap lua chon cua ban : ";
		int thongkeOP;
		cin>>thongkeOP;
		switch(thongkeOP){
			case 1 : 
				float maxgiaphong;
				float mingiaphong;
				maxgiaphong = DS[0].giaphong;
				mingiaphong = DS[0].giaphong;
				for(int i=0;i<n;i++){
					if (DS[i].giaphong >maxgiaphong)
						maxgiaphong =DS[i].giaphong;
				}
				for(int j=0;j<n;j++){
					if (DS[j].giaphong<mingiaphong)
						mingiaphong = DS[j].giaphong;
				}
				cout<<endl;
				cout<<"Phong co gia cao nhat la :"<<endl;
				for (int i=0;i<n;i++){
					if (DS[i].giaphong == maxgiaphong){
						cout<<"ten phong :"<<DS[i].tenphong<<endl;
						cout<<"ma phong : "<<DS[i].maphong<<endl;
						cout<<"gia phong : "<<DS[i].giaphong<<endl;
					}
				}
				cout<<"Phong co gia thap nhat la : "<<endl;
				for (int j=0;j<n;j++){
					if (DS[j].giaphong == mingiaphong){
						cout<<"ten phong :"<<DS[j].tenphong<<endl;
						cout<<"ma phong : "<<DS[j].maphong<<endl;
						cout<<"gia phong : "<<DS[j].giaphong<<endl;
					}
				}
				break;
			case 2 : 
				float maxsongay;
				float minsongay;
				maxsongay = DS[0].tongsongaysudung;
				minsongay = DS[0].tongsongaysudung;
				for(int i=0;i<n;i++){
					if (DS[i].tongsongaysudung >maxsongay)
						maxsongay =DS[i].tongsongaysudung;
				}
				for(int j=0;j<n;j++){
					if (DS[j].tongsongaysudung<minsongay)
						minsongay= DS[j].tongsongaysudung;
				}
				cout<<endl;
				cout<<"Phong duoc su dung nhieu nhat la la :"<<endl;
				for (int i=0;i<n;i++){
					if (DS[i].tongsongaysudung == maxsongay){
						cout<<"ten phong :"<<DS[i].tenphong<<endl;
						cout<<"ma phong : "<<DS[i].maphong<<endl;
						cout<<"so ngay duoc su dung : "<<DS[i].tongsongaysudung<<endl;
					}
				}
				cout<<"Phong co gia thap nhat la : "<<endl;
				for (int j=0;j<n;j++){
					if (DS[j].tongsongaysudung == minsongay){
						cout<<"ten phong :"<<DS[j].tenphong<<endl;
						cout<<"ma phong : "<<DS[j].maphong<<endl;
						cout<<"so ngay duoc su dung : "<<DS[j].tongsongaysudung<<endl;
					}
				}
				break;
			case 3 :
				{
				cout<<"1.Xem bang doanh thu  \n2.Xem phong co doanh thu cao nhat\n3.Xem phong co doanh thu thap nhat\n4.Tro lai"<<endl;
				cout<<"Nhap lua chon cua ban : ";
				int doanhthuOP;
				cin>>doanhthuOP;
				switch(doanhthuOP){
					case 1 : 
						for (int i=0;i<n;i++){
							cout<<"Phong "<<setw(6)<<DS[i].tenphong<<" | ";
							cout<<setw(12)<<DS[i].doanhthu<<endl;
						}
						break;
					case 2:
						float doanhthumax;
						doanhthumax = DS[0].doanhthu;
						for(int j=0;j<n;j++){
							if(DS[j].doanhthu > doanhthumax)
								doanhthumax = DS[j].doanhthu;
						}
						cout<<endl;
						cout<<"Phong co doanh thu cao  nhat la :"<<endl;
						for (int a=0;a<n;a++){
							if (DS[a].doanhthu == doanhthumax){
								cout<<"ten phong :"<<DS[a].tenphong<<endl;
								cout<<"ma phong : "<<DS[a].maphong<<endl;
								cout<<"doanhthu : "<<DS[a].doanhthu<<endl;
							}
						}
						break;
					case 3:
						{
						float doanhthumin;
						doanhthumin= DS[0].doanhthu;
						for(int i=0;i<n;i++){
							if(DS[i].doanhthu < doanhthumin)
								doanhthumin = DS[i].doanhthu;
						}
						cout<<endl;
						cout<<"Phong co doanh thu thap  nhat la :"<<endl;
						for (int j=0;j<n;j++){
							if (DS[j].doanhthu == doanhthumin){
								cout<<"ten phong :"<<DS[j].tenphong<<endl;
								cout<<"ma phong : "<<DS[j].maphong<<endl;
								cout<<"doanhthu : "<<DS[j].doanhthu<<endl;
							}
						}
						break;
						}
					case 4:break;
				break;
			}
			case 4 :
					{ 
				float maxhieuqua;
				float minhieuqua;
				maxhieuqua = DS[0].hieuqua;
				minhieuqua = DS[0].hieuqua;
				for(int i=0;i<n;i++){
					if (DS[i].hieuqua >maxhieuqua)
						maxhieuqua =DS[i].hieuqua;
				}
				for(int j=0;j<n;j++){
					if (DS[j].hieuqua<minhieuqua)
						minhieuqua= DS[j].hieuqua;
				}
				cout<<endl;
				cout<<"Phong co su hieu qua nhat la  :"<<endl;
				for (int i=0;i<n;i++){
					if (DS[i].hieuqua == maxhieuqua){
						cout<<"ten phong :"<<DS[i].tenphong<<endl;
						cout<<"ma phong : "<<DS[i].maphong<<endl;
						cout<<"hieuqua : "<<DS[i].hieuqua<<"%"<<endl;
					}
				}
				cout<<"Phong co su hieu qua it nhat la : "<<endl;
				for (int j=0;j<n;j++){
					if (DS[j].hieuqua == minhieuqua){
						cout<<"ten phong :"<<DS[j].tenphong<<endl;
						cout<<"ma phong : "<<DS[j].maphong<<endl;
						cout<<"hieuqua : "<<DS[j].hieuqua<<"%"<<endl;
					}
				}
				break;
			}
 			case 5 :
			cout<<"Phong"<<" | "<<setw(19)<<"Doanh thu(trVND)"<<" | "<<setw(8)<<"Hieu qua(%)"<<endl;
				for (int i=0;i<n;i++){
							cout<<setw(5)<<DS[i].tenphong<<" | "<<setw(19)<<DS[i].doanhthu/1000000<<" | "<<setw(8)<<DS[i].hieuqua<<endl;
						}				 
						break; 
			case 6 : return;
			
		}
		cout<<"Nhan R de tro lai :";
		cin>>gao;
	}
}
}
void Nhap(phongkhachsan DS[], int n){
		cout<<"1. Nhap du lieu moi.\n2. Nhap du lieu tu file co san.\n3. Thoat\n";
		cout<<"Nhap lua chon cua ban";
		int control;
		cin>> control;
		switch(control)
		{
			case 1:
			
				for(int i=0;i<n;i++)
		    	{
			        cout<<"Nhap vao ten phong: ";
			        cin>>DS[i].tenphong;
			        fflush(stdin);
			        cin.ignore();
			        cout<<"Nhap vao ma phong: ";
			        cin>>DS[i].maphong;
			        fflush(stdin);
			        cin.ignore();
			        cout<<"Nhap vao gia phong: ";
			        cin>>DS[i].giaphong;
			        cout<<"Nhap vao so ngay su dung: ";
					cin>>DS[i].tongsongaysudung;
			        cout<<"Nhap vao thang : ";
			        cin>>DS[i].thang[20];
		    	
		    	}
			
		    	break;			
			
			case 2:{
				
		
				fstream fileInput("danhsachphong.txt");
				if (!fileInput.is_open()){
					cout<<"Khong mo duoc file"<<endl;
					return;  
				}
				int K=12;
				int i=0;
				while(!fileInput.eof()){
					getline(fileInput,DS[i].tenphong);
					if(DS[i].tenphong=="")
						break;
					getline(fileInput,DS[i].maphong);
					if(DS[i].maphong=="")
						break;
					
					fileInput>>DS[i].giaphong;
					
					fileInput>>DS[i].tongsongaysudung;		
					for(int j =0;j<K;j++)
						fileInput>>DS[i].thang[20];
					n++;
					fileInput.ignore();
				}
				fileInput.close();
				cout<<"Thanh cong\n";
			}
				break;
				   
			
			case 3: return;	
		}
	
    
}
void Xem(phongkhachsan DS[]){
	
}
void Sua(phongkhachsan DS[]){
	
}
void Timkiem(phongkhachsan a[],int soluong){
	/*charkeyy;
          do{
				string nhapmsp;
				fflush(stdin);
				cout  << "NHAP MA PHONG CAN TIM :" << endl;
				cin >> nhapmsp;
				thuephong(a, soluong);
       	       	bool out = true;
				for (int i = 0 ; i < soluong ; i ++)
				{			
					if(nhapmsp == a[i].maphong)
					{	
						xuatmotphong(a, a[i], soluong, nhapmsp);
						out = false;
					}	
				}
				if(out)
				{
					cout <<"\n\t khong tim thay phong ban can tim ! \n" << endl;
				}
     	      	fflush(stdin);
       	    	cout << "BAN CO MUON TIM KIEM TIEP K??(Y/N)..." << endl;
       	    	cout << "1: TIEP TUC !!!" << endl;
               	cout << "2: THOAT KHOI CHUONG TRINH!!" << endl;
      	    	cout << "LUA CHON CUA BAN: \t";
              	cin >> keyy;
           		cin.ignore();
               	if (keyy != '1' && keyy != '2')
               	{
     		     	while(keyy != '1' && keyy != '2'){
           	 			cout << "YEU CAU NHAP DUNG LUA CHON!!! ";
       	     			cin >> keyy;
					}
				}
			}while(keyy != '2');
			return;*/
}
void luachoncuaban(phongkhachsan DS[],int N)
{
	char r;
		char xacnhan='Y';
	while(xacnhan=='y'||xacnhan=='Y'){
	int luachon;
	displaymenu();
	cout<<"Nhap lua chon cua ban: ";
	cin>>luachon;
	switch(luachon)
	{
		case 1:Nhap(DS,N);break;
		case 2:Xem(DS);break;
		case 3:Sua(DS);break;
		case 4:Timkiem(DS,N);break;
		case 5:Thongke(DS, N);break;
		case 6: return;	
		default:
			cout<<"Khong hop le"<<"\n"<<"Vui long nhap lai!!"<<"\n";
	}
	cout<<"Nhap y de tiep tuc: ";
	cin>>xacnhan;	
	}
}
int main(){
	int n;
	phongkhachsan DS[100];
	//nhapthongtin(ks);
	//n là số phòng
	nhapfile(DS,n);
	luachoncuaban(DS,n);
	xuatfile(DS,n);
	system("pause");
	return 0;
}

Thông báo lỗi đâu?
Xem có dư hay thiếu cặp ngoặc nào không?!

3 Likes

khi chạy file thì lựa chọn “Nhap du lieu moi” không nhập được ạ

Bạn chưa khởi tạo giá trị cho biến n, nên có thể compliler mặc định nó bằng 0 và khi đó vòng lặp sẽ dừng ngay lúc xét điều kiện lần đầu tiên.

5 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?