comparing two string in if statement

HI i actually i want store string in r and try to compare other string (room_no) in function check but i try many time it still having error can someone help me T_________T


#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<dos.h>
#include<string>
#include<stdlib.h>
#include <iomanip>
#include <string.h>
using namespace std;

class Room
{

public:
const char * room_no;
void menu();
string id, name, phone,email,time;
void book();
int check(const char *);
};


void Room::book()
{
system("cls");
int flag;
const char * r;
ofstream fout("Record.dat",ios::app);
string id, name, phone,email,time;


top:
cout<<"\n\n Room no: ";
cin>>r;
flag=check(r);

if(flag)
cout<<"\n Sorry..!!!Room is already booked";
else
{
room_no=r;
cout<<" Name: ";
cin >>name;
cout<<" ID: ";
cin>>id;
cout<<" Emai: ";
cin >> email;
cout<<" Phone No: ";
cin>> phone;
cout <<" Time: ";
cin >> time;
fout.write((char*)this,sizeof(Room));
cout<<"\n Room is booked...!!!";
}

cout<<"\n Press any key to continue.....!!";
getch();
fout.close();
goto top;
}


int Room::check(const char * r)
{
int flag=0;
ifstream fin("Record.dat",ios::in);
while(!fin.eof())
{
fin.read((char*)this,sizeof(Room));
if(strcmp(r,room_no)==0)
{
flag=1;
break;
}
}

fin.close();
return(flag);
}
write() can't be used to write to a file a struct with pointer members. You need to figure out some other way to save the object.
First, a use of code tags and indentation in posts makes commenting easier.

Second fin.read( (char*)this, sizeof(Room) );
... I sit on a sturdy branch of a tall tree. Reality check states that there is no tree, but a tiny bush. Where do I sit?

It gets better: fout.write( (char*)this, sizeof(Room) );
That could work with POD, but Room has std::string members, and their data is not with the sizeof(Room) bytes that you write out. In other words, you don't save whole Rooms.

Furthermore, your book() introduces local variables with same names as the class members. When you read into 'name', it is not the 'this->name'.


tldr; You have many other design issues than the strcmp.
Topic archived. No new replies allowed.