4 Replies - 979 Views - Last Post: 19 November 2012 - 05:25 PM Rate Topic: -----

#1 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Error

Posted 19 November 2012 - 04:13 PM

I am getting an error stating that occup is not declared. But I already declared it. Can you find why this is happening?

//Lynette Wilkins
//Week 6

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class HotelRoom
{
private:
	char room_num[3]; //Character array that stores a 3-character room number
	char* guest; //Character pointer,which stores the name of the guest occupying the room
	int room_cap;
	int occup_stat;
	double daily_rt;

public:
	HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
	~HotelRoom();
	char* Get_Number();  //Displays room number and add the method Display_Guest()
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	double Change_Rate(double);
	char* Get_Guest();


};

HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
	strcpy(room_num, room); //copy first argument into room_num[]
	guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
	strcpy(guest, g_p); //copy fourth argument into new space
    room_cap = roomcap;
	daily_rt = rate;
	occup_stat = occup;

}



HotelRoom::~HotelRoom()
{
	cout << endl<<endl;
	cout << "Guest in room "<<room_num << " has checked out." <<endl;
	delete [] guest;
}

 char* HotelRoom::Get_Number()
{
	return room_num;
	
}

int HotelRoom::Get_Capacity()
{
	return room_cap;
}

int HotelRoom::Get_Status()
{
	
	return occup_stat;
}



int HotelRoom::Change_Status(int occup)
{
	occup_stat = occup;

	if (occup > room_cap)
	{
		return -1;
	}
	else
	
	return occup_stat;
	
}

double HotelRoom::Get_Rate()
{
	return daily_rt;
}

double HotelRoom::Change_Rate(double rate)
{
	daily_rt = rate;
		return daily_rt;
}


char* HotelRoom::Get_Guest()
{
	return guest;
}


void Display_Room(HotelRoom&);  //Function prototypes
HotelRoom* Create_Room();

int main()
{
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);

	HotelRoom* hotel[200];
	
	int count = 0;
	char response;

	cout<<endl;
	cout<< "Do you want to enter information about a hotel room?(Y/N): ";
	response = cin.get();
	cin.get();      //This will clear the input buffer

	while (toupper(response) == 'Y' && count< 200)
	{
		hotel[count] = Create_Room();
		++count;
		cout<<endl;
		cout<< "Do you want to enter information about a hotel room?(Y/N): ";
		response = cin.get();
		cin.get();   //This will clear the input buffer
	}

	// Display the rooms

	int i;

	for (int i = 0; i< count; ++i)
		Display_Room(*hotel[i]);

	for (i = 0; i < count; ++i)
		delete hotel[i];
	cout<<endl;
	return 0;
}

void Display_Room(HotelRoom& room)
{
	cout<<endl;
	cout<< "Information for room " << room.Get_Number() <<endl<<endl;
	cout<< "Primary Guest Name: " << room.Get_Guest() <<endl;
	cout<< "The amount of guest that will occupy this room is: "<<room.Change_Status(occup) <<endl;
	cout<< "The daily rate of this room is: " <<room.Get_Rate() <<endl;
}

HotelRoom* Setup_Room()
{
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
int occup;
double rate = 89.00;

HotelRoom* room_ptr;

cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;


cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);

cin.get();  //Clear the input buffer of newline character
room_ptr = new HotelRoom (room, buffer, roomcap, occup, rate);

return room_ptr;
}





View Postllwilkins, on 19 November 2012 - 04:10 PM, said:

I am getting an error stating that occup is not declared. But I already declared it. Can you find why this is happening?

//Lynette Wilkins
//Week 6

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class HotelRoom
{
private:
	char room_num[3]; //Character array that stores a 3-character room number
	char* guest; //Character pointer,which stores the name of the guest occupying the room
	int room_cap;
	int occup_stat;
	double daily_rt;

public:
	HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
	~HotelRoom();
	char* Get_Number();  //Displays room number and add the method Display_Guest()
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	double Change_Rate(double);
	char* Get_Guest();


};

HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
	strcpy(room_num, room); //copy first argument into room_num[]
	guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
	strcpy(guest, g_p); //copy fourth argument into new space
    room_cap = roomcap;
	daily_rt = rate;
	occup_stat = occup;

}



HotelRoom::~HotelRoom()
{
	cout << endl<<endl;
	cout << "Guest in room "<<room_num << " has checked out." <<endl;
	delete [] guest;
}

 char* HotelRoom::Get_Number()
{
	return room_num;
	
}

int HotelRoom::Get_Capacity()
{
	return room_cap;
}

int HotelRoom::Get_Status()
{
	
	return occup_stat;
}



int HotelRoom::Change_Status(int occup)
{
	occup_stat = occup;

	if (occup > room_cap)
	{
		return -1;
	}
	else
	
	return occup_stat;
	
}

double HotelRoom::Get_Rate()
{
	return daily_rt;
}

double HotelRoom::Change_Rate(double rate)
{
	daily_rt = rate;
		return daily_rt;
}


char* HotelRoom::Get_Guest()
{
	return guest;
}


void Display_Room(HotelRoom&);  //Function prototypes
HotelRoom* Create_Room();

int main()
{
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);

	HotelRoom* hotel[200];
	
	int count = 0;
	char response;

	cout<<endl;
	cout<< "Do you want to enter information about a hotel room?(Y/N): ";
	response = cin.get();
	cin.get();      //This will clear the input buffer

	while (toupper(response) == 'Y' && count< 200)
	{
		hotel[count] = Create_Room();
		++count;
		cout<<endl;
		cout<< "Do you want to enter information about a hotel room?(Y/N): ";
		response = cin.get();
		cin.get();   //This will clear the input buffer
	}

	// Display the rooms

	int i;

	for (int i = 0; i< count; ++i)
		Display_Room(*hotel[i]);

	for (i = 0; i < count; ++i)
		delete hotel[i];
	cout<<endl;
	return 0;
}

void Display_Room(HotelRoom& room)
{
	cout<<endl;
	cout<< "Information for room " << room.Get_Number() <<endl<<endl;
	cout<< "Primary Guest Name: " << room.Get_Guest() <<endl;
	cout<< "The amount of guest that will occupy this room is: "<<room.Change_Status(occup) <<endl;
	cout<< "The daily rate of this room is: " <<room.Get_Rate() <<endl;
}

HotelRoom* Setup_Room()
{
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
int occup;
double rate = 89.00;

HotelRoom* room_ptr;

cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;


cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);

cin.get();  //Clear the input buffer of newline character
room_ptr = new HotelRoom (room, buffer, roomcap, occup, rate);

return room_ptr;
}





This error is in line 151

Is This A Good Question/Topic? 0
  • +

Replies To: Error

#2 Ryokuro  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 17
  • Joined: 16-June 11

Re: Error

Posted 19 November 2012 - 04:19 PM

from what i see you have occup going out of scope and it is not defined within the function you are trying to use it in.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Error

Posted 19 November 2012 - 04:22 PM

Where in the following snippet have you defined your variable?

void Display_Room(HotelRoom& room)
{
	cout<<endl;
	cout<< "Information for room " << room.Get_Number() <<endl<<endl;
	cout<< "Primary Guest Name: " << room.Get_Guest() <<endl;
	cout<< "The amount of guest that will occupy this room is: "<<room.Change_Status(occup) <<endl;
	cout<< "The daily rate of this room is: " <<room.Get_Rate() <<endl;
}


You may want to review variable scope.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Error

Posted 19 November 2012 - 04:50 PM

View Postjimblumberg, on 19 November 2012 - 04:22 PM, said:

Where in the following snippet have you defined your variable?

void Display_Room(HotelRoom& room)
{
	cout<<endl;
	cout<< "Information for room " << room.Get_Number() <<endl<<endl;
	cout<< "Primary Guest Name: " << room.Get_Guest() <<endl;
	cout<< "The amount of guest that will occupy this room is: "<<room.Change_Status(occup) <<endl;
	cout<< "The daily rate of this room is: " <<room.Get_Rate() <<endl;
}


You may want to review variable scope.

Jim


I declared it here:


HotelRoom* Setup_Room()
{
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
int occup;
double rate = 89.00;

HotelRoom* room_ptr;




So I thought this would declare it for this part:

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;



Then I thought that occup would be pulled from here

room_ptr = new HotelRoom (room, buffer, roomcap, occup, rate);
and moved to the function and then so on. All of the other ones worked correctly I just didn't understand why this one display did not work correctly.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Error

Posted 19 November 2012 - 05:25 PM

Because those two places are in a different scope. Did you read the link I provided?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1