why there is error in this statement?

Mar 20, 2011 at 8:05pm
b.getavailable()=0;
Mar 20, 2011 at 8:08pm
well, it looks like you're trying to assign a value to a function call
Mar 20, 2011 at 8:12pm
yes i want to but the new value in this function because it will return avariable that i want to change it......
Mar 20, 2011 at 8:14pm
You'd need to return a reference (type&) to make that work...

-Albatross
Mar 20, 2011 at 8:16pm
why i can not assign this function...???
Mar 20, 2011 at 8:17pm
The same reason you can't do this:
1 = 0;

:/

EDIT: Well, almost. What I'm guessing what your getavailable is doing is returning a value, which you're then trying to assign another error, resulting in something similar to what I showed above. You'd need to return a reference to get the desired effect.

-Albatross
Last edited on Mar 20, 2011 at 8:19pm
Mar 20, 2011 at 8:19pm
can you show us the function definition? or at least tell us the return type?
Mar 20, 2011 at 8:29pm
thanks for help me i understand it now....

this is my function definition
1
2
int getavailable()
{ return available;}
Last edited on Mar 20, 2011 at 8:50pm
Mar 20, 2011 at 8:30pm
how can i return reference??
i mean the syntax rule..
Mar 20, 2011 at 8:37pm
if you actually want to assign 0 to the result of the function, do this:
1
2
int& getavailable()
{ return available; }


after your function call, available will hold the value 0.
Last edited on Mar 20, 2011 at 8:37pm
Mar 20, 2011 at 8:39pm
closed account (3hM2Nwbp)
I might be missing something, but isn't that what a setter is used for?

 
b.setavailable(0);
Mar 20, 2011 at 8:47pm
yes, what i wrote is equivalent to
1
2
void getavailable (int x) 
{ available = x; }


if that's not what you're trying to do, then what? are you trying to find out whether or not the function is returning 0?
Mar 20, 2011 at 8:49pm
yes i forget the set function but i can use get function to do the same is that right?
thanks again for help me...
it's clear now to me..
Last edited on Mar 20, 2011 at 8:50pm
Mar 20, 2011 at 8:54pm
are you saying you want the function to be a getter? if so, then you need to either print out the result of the function or assign it to a variable.
std::cout << b.getavailable();
or
someInt = b.getavailable();
i'm not entirely sure what you're asking, though
Mar 20, 2011 at 9:05pm
can i post my program here..
if you want to understand what i am doing....
Mar 20, 2011 at 9:08pm
this is the coding Exercise?
A small hotel tries to build a new system to keep information to facilitate
the searching process for available rooms. It has a number of rooms (assume
that it has only 10 room); the hotel database contains each room information
and a list of available rooms.
Each room contains the following ( room number, room type "single or
couple" , with a view or not, and its price)
Your program should include the following functions:
1. Build hotel information:
By calling this function , the information of each room will be entered.
Also suppose at the beginning that all of rooms are available.
2. Reserve a room:
When a new customer come ,the receptionist will ask the customer
about his/her preference ( room type "single or couple", with a view or
not) .Then, the receptionist will search for a suitable room . finally,
providing the customer with the cost of selected room (note that any
room costs 500SR , if it's couple add 100SR , and if it's with a view
add another 100SR) .
3. print Information of all "single" rooms:
Mar 21, 2011 at 4:57pm
this is my code I think there is a logical error but I don't know where....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
using namespace std;
#include<string>


class room{

int roomNum;
string roomType;// single or couple
bool with_a_view;// 0 or 1
double price;
int available;
public:
	room()
		{available=1;
		price=0.0;
		with_a_view=0;
		roomType="  ";
		}
	void build_hotel_information()
	
	
	{
		cout<<"enter the number of room"<<endl;
	cin>>roomNum;
	cout<<"enter room type (single or couple):"<<endl;
	cin>>roomType;
	cout<<"with a view or not:press(0) for no and press (1) for yes : "<<endl;
	cin>>with_a_view;
	
	if(roomType=="couple"&&with_a_view==1)
		price=500+100+100;
	if(roomType=="couple"&&with_a_view==0)
		price=500+100;
	if(roomType=="single"&&with_a_view==1)
		price=500+100;
	else
		price=500;
	}
int getroomNum()
	{return roomNum;
}
string getroomType()
{ return roomType;}
bool getwith_a_view()
{ return  with_a_view;}
double getprice()
{ return price;}
int& getavailable()
{ return available;}



};//end of class
	

class hotel{
	room r[10];
	string type;
	bool view;
public:

		
	void Reserve_room()


	{
		for(int i=0;i<10;i++)
	{r[i].build_hotel_information();
	r[i].getavailable();
	r[i].getprice();
	r[i].getroomNum();
	r[i].getroomType();
	r[i].getwith_a_view();
	}
		
		cout<<"enter the room type you want (single or couple): "<<endl;
	cin>>type;
	cout<<"Do you want it with a view or not? :press(0) for no and press (1) for yes : "<<endl;
	cin>>view;
		
		
		for(int j=0;j<10;j++)
	{
		if(type==r[j].getroomType())
	if(view==r[j].getwith_a_view())
		if(r[j].getavailable()==1){
				r[j].getavailable()=0;
	cout<<"your room is:"<<r[j].getroomNum()<<endl;
	cout<<"the room cost: "<<r[j].getprice()<<endl;
		      else
			cout<<"sorry there is no rooms available"<<endl;
		}
		

		}
	}
};

	int main()
	{
	hotel h1;
	h1.Reserve_room();
return 0;	
	}
Topic archived. No new replies allowed.