C++ Help

Boomer151

Junior Member
Jun 25, 2001
12
0
0
Third problem area.
Room Fee.
cout << "Your room fee is:" << setw(21) << "$" << room_fee << endl;
cout << endl;

Day of event.
cout << "Enter the day of your event:";
cin >> week_day;
if (week_day == 'Monday' || week_day == 'monday' || week_day == 'Tuesday' || week_day == 'tuesday' || week_day == 'Wednesday' || week_day == 'wednesday' || week_day == 'Thursday' || week_day == 'thursday')
{
cout << "No extra fee";
}
else if ( week_day == 'Friday' || week_day == 'friday' || week_day == 'Saturday' || week_day == 'saturday' || week_day == 'Sunday' || week_day == 'sunday')
{
cout << "You will have a 7% fee added to your bill";
}

Here are the errors Visual C++ outputs.
--------------------Configuration: hw6 - Win32 Debug--------------------
Compiling...
hw6.cpp
C:\Documents and Settings\Neal E. Williams\Desktop\hw6\hw6.cpp(122) : error C2015: too many characters in constant
etc thru line 126.

hw6.obj - 14 error(s), 0 warning(s)
 

manly

Lifer
Jan 25, 2000
13,086
3,851
136
Match the errors to the line numbers (122 and 126) in your source code and figure out what the problem is.

The error message suggests that you're incorrectly using the single quote symbol on a string literal. Looking at your code though, it appears you commented out that section of code. What I mean by single quote symbol on a string literal is where you have 'Monday' instead of "Monday".

As for why it always prints out 55.00, you're incorrectly using the logical OR operator.

Instead of (room == 'A' || 'a') you have to do (room == 'A' || room == 'a')

The first (incorrect) usage still compiles and executes, but it's a bug. In C/C++ 0 is false, and any non-zero value is true. 'a' is a non-zero value, and it evaluates to true. So the expression (room == 'A' || 'a') evaluates to (room == 'A' || true). I'm not explaining this all that well, so ask again if you need clarification.

Instead of using cascading if/else if statements, you could use switch:

switch (room) {
case 'A':
case 'a':
// cout Room fee for A
break;

case 'B':
case 'b':
// cout Room fee for B

...
default:
cout << "Invalid Room Code.\n";
}

An even better way is instead of hard-coding all those cout lines separately, I would use an array of room costs.

For example:

double roomCost[] = { 55.00, 70.00, 85.00 };
...
if (room code is valid) {
print out getRoomCost(room)
}
else {
print out "Invalid Room Code."
}

The function getRoomCost does a lookup of the room code in the roomCost array.

Instead of showing valid C code, I decided it's better to show pseudo-code.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Wow, that's a long post!
You might try trimming it down to the strictly relevant sections next time.

The problem you're having with the meal_cost stuff is probably related to trying to do equality comparisons on floating point values.
My guess is that 15.80 cannot be represented exactly by a binary representation ... it will actually be something like 15.80000001, which isn't equal to 15.80.

You might try something like this:
if(int(meal_cost*100) == 1580)

Multiplying by 100 and casting to int will truncate the value at two decimal places.

Also, you don't need the if() in the last block of that statement, but if you do use it, it should be and (&&) not or (II).

On the room business, you have:
if (room == 'A' || 'a')

it should be:
if (room == 'A' || room == 'a')

The day of event problem is similar
 

Boomer151

Junior Member
Jun 25, 2001
12
0
0
The reason I am doing the way I am is due to the fact we are not allowed to use array's and I have not learned them yet.

Thank you for the help. As you can see I am not the cleanest coder and always froget things.

Again thx man.
 

manly

Lifer
Jan 25, 2000
13,086
3,851
136
String literals need to be surrounded by double quotes, i.e. "Monday" instead of 'Monday'

Also, generally speaking, you cannot compare String literals using the == operator.

You need to use strncmp() from the C standard library.