if statement problems

here is my code. I can't get it to work if i pick 2 for room and 7 for floor it should be 72.00 but it says 55.00 and i can't fingure out why. Please help me.

#include<iostream>
using namespace std;
int main()
{
int room;
int floor;
int night;
cout << "Please enter the type of room you would like. 1 for single, "
<< "2 for a double, or 3 for a suite " << endl;
cin >> room;
if(room == 1)
cout << "You would like a single " << endl;
else
if(room == 2)
cout << "You would like a double " << endl;
else
if(room == 3)
cout << "You would like a suite " << endl;
else
cout << "Please try again. You need to pick between 1,2 and 3 " << endl;
cout << "Please enter the floor you wish to stay on, please enter from 1 though 12 " << endl;
cin >> floor;
if(floor == 1)
cout << "You wish to stay on floor 1 " << endl;
else
if(floor == 2)
cout << "You wish to stay on floor 2 " << endl;
else
if(floor == 3)
cout << "You wish to stay on floor 3 " << endl;
else
if(floor == 4)
cout << "You wish to stay on floor 4 " << endl;
else
if(floor == 5)
cout << "You wish to stay on floor 5 " << endl;
else
if(floor == 6)
cout << "You wish to stay on floor 6 " << endl;
else
if(floor == 7)
cout << "You wish to stay on floor 7 " << endl;
else
if(floor == 8)
cout << "You wish to stay on floor 8 " << endl;
else
if(floor == 9)
cout << "You wish to stay on floor 9 " << endl;
else
if(floor == 10)
cout << "You wish to stay on floor 10 " << endl;
else
if(floor == 11)
cout << "You wish to stay on floor 11 " << endl;
else
if(floor == 12)
cout << "You wish to stay on floor 12 " << endl;
else
cout << "Try again. Please enter a number between 1 and 12" << endl;
cout << "Please enter the number of nights you wish to stay " << endl;
cin >> night;
if(room == 1 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
cout << "The single night price for your room is 45.00 " << endl;
else
if(room == 2 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
cout << "The single night price for your room is 60.00 " << endl;
else
if(room == 3 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
cout << "The single night price for your room is 130.00 " << endl;
else
if(room == 1 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
cout << "The single night price for your room is 55.00 " << endl;
else
if(room == 2 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
cout << "The single night price for you room is 72.00 " << endl;
else
if(room == 3 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
cout << "The single night price for you room is 215.00 " << endl;
system("pause");
return 0;


You should parenthesize your conditions
eg:
( room == 1 && ( floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5 ) )
thanks i tried that once but i'm sure i did it wrong. I suck at c++.
sorry for my english...

actually if you pick room 2 and floor 7
the first condition that is true valued is
if(room == 1 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
thats way is printed out cout << "The single night price for your room is 55.00 " << endl;

take a look
&& and || have the same precedence
then if
room == 1 is false
floor == 6 is false
floor == 7 is true
and the other conditions doesn't matter
what we got
false && false || true || x || x || is true if you evaluate it from left to rigth
false &&false = false
false || true = true
true || x = true...
take the precedence in mind...
i got it working but i have to show a 10% discount if the nights stayed is over 4 how would i go about doing that please. i know to multipy and then subtract 10% but the room price is not saved anywhere.
Last edited on
Instead of cout-ing all of those room quotes, instead assign the room price to a variable. Then after all of your if statements, announce the price using the variable. You will then have it for computing a percentage.

~psault
I was thinking something like that but i just can't get it to work i tried using cin and making variables for the price but just gave me a blank line.
i don't understand why you can't do that about the variable...
this is how i think must be done

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
#include<iostream>
using namespace std;
int main(int na, char* args[])
{	int room , floor , night ;
	float  price ;
	
	//here you insert the instructions  
	cout << "Please enter the type of room you would like. 1 for single, "
		<< "2 for a double, or 3 for a suite " << endl;
	cin >> room;
	if(room == 1)
		cout << "You would like a single " << endl;
	else if(room == 2)
		cout << "You would like a double " << endl;
	else if(room == 3)
		cout << "You would like a suite " << endl;
	else
		cout << "Please try again. You need to pick between 1,2 and 3 " << endl;
	//all this instructions must be placed into an do{... }while( room<1 || room>3)  cycle
	
	cout << "Please enter the floor you wish to stay on, please enter from 1 though 12 " << endl;
	cin >> floor	;

	//what follows it's unnecessarily complicated
	if(floor == 1)
		cout << "You wish to stay on floor 1 " << endl;
	else if(floor == 2)
		cout << "You wish to stay on floor 2 " << endl;
	else if(floor == 3)
		cout << "You wish to stay on floor 3 " << endl;
	else if(floor == 4)
		cout << "You wish to stay on floor 4 " << endl;
	else if(floor == 5)
		cout << "You wish to stay on floor 5 " << endl;
	else if(floor == 6)
		cout << "You wish to stay on floor 6 " << endl;
	else if(floor == 7)
		cout << "You wish to stay on floor 7 " << endl;
	else if(floor == 8)
		cout << "You wish to stay on floor 8 " << endl;
	else if(floor == 9)
		cout << "You wish to stay on floor 9 " << endl;
	else if(floor == 10)
		cout << "You wish to stay on floor 10 " << endl;
	else if(floor == 11)
		cout << "You wish to stay on floor 11 " << endl;
	else if(floor == 12)
		cout << "You wish to stay on floor 12 " << endl;
	else 
                cout << "Try again. Please enter a number between 1 and 12" << endl;

	/* instead
		if ( floor<1 ||  floor>12)
			cout << "Try again. Please enter a number between 1 and 12" << endl;
		else
			cout << "You wish to stay on floor " << floor << endl;
	*/
	// newly this must be placed at a do... while( 1>floor ||  floor>12 ) cycle

	cout << "Please enter the number of nights you wish to stay " << endl;
	cin >> night;

	//what follows it's unusefull for discount porpuse
	if(room == 1 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
			cout << "The single night price for your room is 45.00 " << endl;
	else 	if(room == 2 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
			cout << "The single night price for your room is 60.00 " << endl;
	else 	 if(room == 3 && floor == 1 || floor == 2 || floor == 3 || floor == 4 || floor == 5)
			cout << "The single night price for your room is 130.00 " << endl;
	else 	if(room == 1 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
			cout << "The single night price for your room is 55.00 " << endl;
	else		if(room == 2 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
			cout << "The single night price for you room is 72.00 " << endl;
	else		if(room == 3 && floor == 6 || floor == 7 || floor == 8 || floor == 9 || floor == 10 || floor == 11)
			cout << "The single night price for you room is 215.00 " << endl;


		/* instead
		if( 1 <= floor && floor <= 5)
		{	//low prices i suppose you don't know switch statement
			if (room ==1 )	price = 45 ;
			if (room ==2 )	price = 60 ;
			if (room ==3 )	price = 130 ;
		}
		else
		{	//high prices
			if (room ==1 )	price = 55 ;
			if (room ==2 )	price = 72 ;
			if (room ==3 )	price = 215 ;
		}
		cout << "The single night price for you room is " << price << endl;
		*/

		/* what you need
		cout << "your account is: " << (price * night) ;
		if (night > 4 )
		{	cout << "you have a discount of 10% : " << (price*night*0.1) <<'\n'
				<< "your total bill: " << (price*night)*(0.9) ;
		}
		*/
	
	return 0 ;
}
Just one last thing. When i try to get a single room with 1 on floor 12 there should be a error and there is, but it also does a bunch on random numbers that shouldn't be there. here is my code.

#include<iostream>
using namespace std;
int main()
{
int room;
int floor;
int night;
float price;
cout << "Please enter the type of room you would like. 1 for single, "
<< "2 for a double, or 3 for a suite " << endl;
cin >> room;
if(room == 1)
cout << "You would like a single " << endl;
else
if(room == 2)
cout << "You would like a double " << endl;
else
if(room == 3)
cout << "You would like a suite " << endl;
else
cout << "Please try again. You need to pick between 1,2 and 3 " << endl;
cout << "Please enter the floor you wish to stay on, please enter from 1 though 12 " << endl;
cin >> floor;
if(floor < 1 || floor > 12)
cout << "Try again. Please enter a number between 1 and 12" << endl;
else
cout << "You wish to stay on floor " << floor << endl;
cout << "Please enter the number of nights you wish to stay " << endl;
cin >> night;
if( 1 <= floor && floor <= 5)
{
if (room ==1 ) price = 45 ;
if (room ==2 ) price = 60 ;
if (room ==3 ) price = 130 ;
}
else
if(6 <= floor && floor <= 11)
{
if (room ==1 ) price = 55 ;
if (room ==2 ) price = 72 ;
if (room ==3 ) price = 215 ;
}
else
if(12 == floor)
{
if (room == 1 ) cout << "Sorry, we don't have single rooms on the 12th floor " << endl;
if (room == 2 ) price = 120 ;
if (room == 3 ) price = 350 ;
}
cout << "The single night price for you room is " << price << endl;
cout << "your account is: " << (price * night) ;
if (night > 4 )
{ cout << "you have a discount of 10% : " << (price*night*0.1) << endl;
cout << "your total bill: " << (price*night)*(0.9) << endl;
}
system("pause");
return 0;
}
Topic archived. No new replies allowed.