繁体   English   中英

标志条件和文件写入

[英]Flag condition and file writing

我正在为我的大学项目创建一个酒店管理系统软件。代码有两个主要问题。

  1. 该程序接受一个房间号,通过检查 function 运行它,如果根据条件预订它,但是当我尝试使用唯一的房间号再次预订时,它说它已被预订,即使它没有在现实中.它只预订一次房间。
  2. book_rooms 的 function 没有将内容写入文件。
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<string.h>
using namespace std;
struct hotel
{
    int room_no[50];
    string name[50] ;
    string adress[50];
    int mobile_no[50];
    int bill[50];
    int days[50];
    int room;
    int room1;
    int flag=0;
    fstream records;

void main_menu();
int book_rooms();
int customer_records();
int rooms_alloted();
int edit_records();
int customer_bills();
int check(int);
};

void hotel :: main_menu()
{
    int choice;
while(choice!=6)
{

  system("cls");
cout<<"\n\t\t\t\t*************************";
cout<<"\n\t\t\t\t HOTEL MANAGEMENT SYSTEM ";
cout<<"\n\t\t\t\t      * MAIN MENU *";
cout<<"\n\t\t\t\t*************************";
cout<<"\n\n\n\t\t\t1.Book A Room";
cout<<"\n\t\t\t2.Customer Records";
cout<<"\n\t\t\t3.Rooms Allotted";
cout<<"\n\t\t\t4.Edit Record";
cout<<"\n\t\t\t5.Customer Bills";
cout<<"\n\t\t\t6.Exit";
cout<<"\n\n\t\t\tEnter Your Choice: ";
cin>>choice;
switch (choice)
{
case 1:
    {
        book_rooms();
        break;
    }
    case 2:
    {
        customer_records();
        break;
    }
    case 3:
    {
        rooms_alloted();
        break;
    }
    case 4:
    {
        edit_records();
        break;
    }
    case 5:
    {
        customer_bills();
        break;
    }
    case 6:
    {
        break;
    }
}

}
}
int hotel:: book_rooms()
{
    
    system("cls");
    int room;
    cout<<"\n\t*************************";
    cout<<"\n\t     ROOM BOOKING";
    cout<<"\n\t*************************";
    cout<<"\n\nPlease enter the room number you want to book.";
    cout<<"\n1: Standard room(Room 1-20) (Rs.1000/night)";
    cout<<"\n2.Suite(Room 21-40) (Rs.5000/night)";
    cout<<"\n3.Luxury Room(Room 41-50) (Rs.10000/night)\n";
    ofstream records("rooms.txt");



    cin>>room;
    room1=room-1;

    flag=check(room);



    if(flag==1)
    {
        cout<<"Sorry this room has been taken";
        system("pause");
    }

    else{

            room_no[room-1]=room;

    records<<"Room "<<room_no[room-1]<<"\n";
    cout<<"Name:";
    getline(cin, name[room-1]);
    getline(cin, name[room-1]);
    records<<name[room-1]<<"\n";
    cout<<"Adress:";
    getline(cin, adress[room-1]);
    records<<adress[room-1]<<"\n";
    cout<<"\nMobile no: ";
    cin>>mobile_no[room-1];
    records<<mobile_no[room-1]<<"\n";
    records.close();
    cout<<"\nYour room has been booked\n";
    system("pause");
}}
int hotel::check(int r)
{
ifstream records("rooms.txt");
while(!records.eof())
{


records>>room_no[r-1];
if(room_no[r-1]==r)
    {
    flag=1;
    break;

    }
    }

records.close();
return flag;

}




int main()
{
    hotel p;
    p.main_menu();
}

此代码似乎很可能存在多个问题。 但是您的直接问题似乎是您同时打开"rooms.txt"进行输入和 output。

int hotel::book_rooms()
{
    ...
    ofstream records("rooms.txt");
    ...
    flag=check(room);
    ...
}

int hotel::check(int r)
{
    ifstream records("rooms.txt");
    ...
}

看到问题了吗? book_rooms立即为 output 打开 rooms.txt,然后check尝试打开相同文件进行输入。 像这样重新组织你的代码,这样你就不会同时在两个地方打开文件。

int hotel::book_rooms()
{
    ...
    flag=check(room);
    ...
    ofstream records("rooms.txt");
    ...
}

int hotel::check(int r)
{
    ifstream records("rooms.txt");
    ...
}

但正如我所说,我认为您会发现这只是该代码的多个问题中的第一个。 看来您正试图过于雄心勃勃,并且在没有足够测试的情况下编写了太多代码。 这是初学者非常常见的失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM