WHAT IS WRONG WITH MY CODING?

Why do I got RM 0 for the total charges?


/// This program that calculates the total cost of a hotel stay.

///The daily base charge is as follow:
/// Deluxe Double Room RM550
/// Deluxe Room RM400
/// Standard Room RM300

/// The hotel also charges for the following optional daily services:
/// Laundry RM20 per day
/// Extra Towel rm10 per day
/// Transport RM50 per day


#include <iostream>
using namespace std;
float calcBaseCharges,days,roomType,calcServiceCharge,laundryCost,extratowelCost,transportCost,calcTotalCharges,aseCharges,serviceCharges;
int main()

{
int days;
int roomType;
double roomPerDay;

double baseCharges;
double serviceCharges;
double totalCharges;

char laundry;
char extratowel;
char transport;

double laundryCost;
double extratowelCost;
double transportCost;


do{
cout << "How many days do you want to spend in the hotel? ";
cin >> days;


if (days < 1)
{
cout << "Please enter a valid number." << endl;
}
else
break;
}while (days >= 1);



do {
cout << "Please choose the room from the following options: " << endl;
cout << "Enter 1 for the Deluxe Double Room. " << endl;
cout << "Enter 2 for the Double Room. " << endl;
cout << "Enter 3 for the Standard Room. " << endl;
cin >> roomType;



if (roomType < 1 || roomType > 3)
{
cout << "Please enter a valid number." << endl;
}
else
break;
}while (roomType >= 1 || roomType <= 3);



switch (roomType)
{
case 1:
roomPerDay = 550;
break;
case 2:
roomPerDay = 400;
break;
case 3:
roomPerDay = 300;
break;
default:
return 1;
}



cout << "Enter 'Y' for yes or 'N' for no for the following optional services: " << endl;

do{
cout << "Do you want a laundry service during your stay? ";
cin >> laundry;
if (laundry == 'Y' || laundry == 'y')
{
laundryCost=20;
break;
}
else if (laundry == 'N' || laundry == 'n')
{
laundryCost=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');



do{
cout << "Do you want an extra towel during your stay? ";
cin >> extratowel;
if (extratowel == 'Y' || extratowel == 'y')
{
extratowelCost=10;
break;
}
else if (extratowel == 'N' || extratowel == 'n')
{
extratowelCost=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (extratowel != 'Y' && extratowel != 'y' && extratowel != 'N' && extratowel != 'n');



do{
cout << "Do you want transportation service during your stay? ";
cin >> transport;
if (transport == 'Y' || transport == 'y')
{
transport=50;
break;
}
else if (transport == 'N' || transport == 'n')
{
transport=0;
break;
}
else
{
cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
}}
while (transport != 'Y' && transport != 'y' && transport != 'N' && transport != 'n');

cout << "The Total charges are RM" << totalCharges << endl;
cout << endl;

{
baseCharges = days * roomType;
return baseCharges;
}
{
serviceCharges = days *(laundryCost + extratowelCost + transportCost);
return serviceCharges;
}
totalCharges = baseCharges + serviceCharges;
return totalCharges;
}


Hello yasab,

To start with:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


While I set up your code for an easier look.

Andy
Hello yasab,

Some things I see without running the program.

Consider writing your code like this:
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/// This program that calculates the total cost of a hotel stay.

///The daily base charge is as follow:
/// Deluxe Double Room RM550
/// Deluxe Room RM400
/// Standard Room RM300

/// The hotel also charges for the following optional daily services:
/// Laundry RM20 per day
/// Extra Towel rm10 per day
/// Transport RM50 per day

#include <iostream>

using namespace std;

double calcBaseCharges, days, roomType, calcServiceCharge, laundryCost, extratowelCost, transportCost, calcTotalCharges, aseCharges, serviceCharges;

int main()
{
    int days{};  // <--- ALWAYS initialize all your variables.
    int roomType{};
    double roomPerDay{};

    double baseCharges{};
    double serviceCharges{};
    double totalCharges{};

    char laundry{};
    char extratowel{};
    char transport{};

    double laundryCost{};
    double extratowelCost{};
    double transportCost{};

    do
    {
        cout << "How many days do you want to spend in the hotel? ";
        cin >> days;

        if (days < 1)
        {
            cout << "Please enter a valid number." << endl;
        }
        else
            break;
    } while (days >= 1);

    do
    {
        cout << 
            "Please choose the room from the following options:\n"
            " Enter 1 for the Deluxe Double Room.\n"
            " Enter 2 for the Double Room.\n"
            " Enter 3 for the Standard Room.: ";
        cin >> roomType;

        if (roomType < 1 || roomType > 3)
        {
            cout << "Please enter a valid number." << endl;
        }
        else
            break;
    } while (roomType >= 1 || roomType <= 3);

    switch (roomType)
    {
        case 1:
            roomPerDay = 550;
            break;
        case 2:
            roomPerDay = 400;
            break;
        case 3:
            roomPerDay = 300;
            break;
        default:
            return 1;
    }

    cout << "Enter 'Y' for yes or 'N' for no for the following optional services:\n";

    do
    {
        cout << "Do you want a laundry service during your stay? ";
        cin >> laundry;

        if (laundry == 'Y' || laundry == 'y')
        {
            laundryCost = 20;
            break;
        }
        else if (laundry == 'N' || laundry == 'n')
        {
            laundryCost = 0;
            break;
        }
        else
        {
            cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
        }
    } while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');

    do
    {
        cout << "Do you want an extra towel during your stay? ";
        cin >> extratowel;

        if (extratowel == 'Y' || extratowel == 'y')
        {
            extratowelCost = 10;
            break;
        }
        else if (extratowel == 'N' || extratowel == 'n')
        {
            extratowelCost = 0;
            break;
        }
        else
        {
            cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
        }
    } while (extratowel != 'Y' && extratowel != 'y' && extratowel != 'N' && extratowel != 'n');

    do
    {
        cout << "Do you want transportation service during your stay? ";
        cin >> transport;

        if (transport == 'Y' || transport == 'y')
        {
            transport = 50;
            break;
        }
        else if (transport == 'N' || transport == 'n')
        {
            transport = 0;
            break;
        }
        else
        {
            cout << "You entered an invalid answer. Please enter 'Y' for yes or 'N' for no. " << endl;
        }
    } while (transport != 'Y' && transport != 'y' && transport != 'N' && transport != 'n');

    cout << "The Total charges are RM" << totalCharges << '\n';
    cout << endl;

    //{  // <--- These {}s are not needed.
        baseCharges = days * roomType;
        return baseCharges;
    //}

    //{  // <--- These {}s are not needed.
        serviceCharges = days * (laundryCost + extratowelCost + transportCost);
        return serviceCharges;
    //}

    totalCharges = baseCharges + serviceCharges;

    return totalCharges;
}

In the 1st do/while your condition is: while (days >= 1);. So if I enter 4 the loop will continue until you enter (0)zero or a negative number. What you want is more like while (days < 1);.

The 2nd do/while is the same. A proper choice will keep you in the loop. What you want is an invalid choice to stay in the loop.

In line 147 you output the total changes, but up to this point "totalCharges" has never received a value and if you did not initialize the variable when you will either an error or a garbage value sent to the screen.

Lines 152, 157 and 162 all return a value, but since the only function is "main" the return statements leave the program.

In line 160 "totalCharges" finally gets a value, but it is to late to use it other than to see what the program returns when it ends. Also the values being returned in lines 152, 157 and 162 are "double"s, but "main" returns an "int".

Andy
Those little brace-bracketed lines of code at the end look like "headless" functions.
Hello yasab,

As a start to fixing the program I have done this:
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
#include <iostream>
#include <limits>

using namespace std;  // <--- Best not to use.

// <--- Try to avoid using global variables. Also these are defined in "main" and never used.
//double calcBaseCharges, days, roomType, calcServiceCharge, laundryCost, extratowelCost, transportCost, calcTotalCharges, aseCharges, serviceCharges;

int main()
{
    int days{};  // <--- ALWAYS initialize all your variables.
    int roomType{};
    double roomPerDay{};

    double baseCharges{};
    double serviceCharges{};
    double totalCharges{};

    char laundry{};
    char extratowel{};
    char transport{};

    double laundryCost{};
    double extratowelCost{};
    double transportCost{};

    while (std::cout << "How many days do you want to spend in the hotel? " && !(cin >> days) || days < 1)
    {
        if (!std::cin)
        {
            std::cerr << "\n     Invalid Input! Must be a number.\n\n";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }
        else if (days < 1)
        {
            std::cerr << "\n     Please enter a valid number.\n\n";
        }
    }

Working on your code is done a little at a time just like the way you should have written it.

I changed the do/while loop to a while loop which works better. Also since "days" is defined as a numeric variable entering a non numeric value will cause "cin" to fail and be unusable until it is fixed, the if statement in the loop.

This type of while loop is something that you should get use to early.

Following this part I adjusted the next "cout" to:
1
2
3
4
5
6
7
8
cout << 
    "\n"
    " Please choose the room from the following options:\n"
    "  Enter 1 for the Deluxe Double Room.\n"
    "  Enter 2 for the Double Room.\n"
    "  Enter 3 for the Standard Room.\n"
    "   Enter choice: ";
cin >> roomType;

The spaces at the beginning of the strings are not necessary, but I find it easier to read this way.

The next do/while should look similar to the 1st one.

After the switch you need to have totalCharges = days * roomPerDay; and after that totalCharges += ???; as you add additional charges.

Then when each part is working you can put the whole thing in a do/while or while loop.

Andy
Try to avoid using global variables...

Something strange about those globals are the ones starting with "calc" which sound like the names of the missing function heads, and the other's are the needed parameters. Strange.
@dutch,

Very true, but as written they appear to be regular variables.

Andy
I said it was strange. ;-)
Hello yasab,

My revised program starts with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    constexpr double
        DD_ROOM_COST { 550.0 },
        DBL_ROOM_COST{ 400.0 },
        STD_ROOM_COST{ 300.0 },
        LAUNDRY_COST {  20.0 },
        TOWEL_COST   {  10.0 },
        TRANS_COST   {  50.0 };

    // <--- These are no longer needed.
    //double laundryCost{};
    //double extratowelCost{};
    //double transportCost{}; 


The 2nd while loop is a different format:
1
2
3
4
5
6
7
8
9
while
    (cout <<
        "\n"
        " Please choose the room from the following options:\n"
        "  Enter 1 for the Deluxe Double Room. @RM" << DD_ROOM_COST << "\n"
        "  Enter 2 for the Double Room.        @RM" << DBL_ROOM_COST << "\n"
        "  Enter 3 for the Standard Room.      @RM" << STD_ROOM_COST << "\n"
        "   Enter choice: " &&
        !(cin >> roomType) || roomType < 1 || roomType > 3)

Notice how I used the constant variables to show the cost of each type of room. This way if any of the prices change you only have 1 place to change them.

Following the switch I added: totalCharges = days * roomPerDay;.

In the next parts the do/while loops work, so I left them alon with minor changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cout << "\n Enter 'Y' for yes or 'N' for no for the following optional services:\n";

do
{
    cout << "\n Do you want a laundry service during your stay? (RM" << LAUNDRY_COST << "): ";
    cin >> laundry;

    if (laundry == 'Y' || laundry == 'y')
    {
        totalCharges += LAUNDRY_COST;

        break;
    }
    else if (laundry == 'N' || laundry == 'n')
    {
        //laundryCost = 0;  // <--- This line is not needed.
        break;
    }
    else
    {
        cerr << "\n     You entered an invalid answer. Please enter 'Y' for yes or 'N' for no.\n";
    }
} while (laundry != 'Y' && laundry != 'y' && laundry != 'N' && laundry != 'n');

When you finish all the do/while loops "totalCharges" will have the proper value.

This is 1 possible test run:

 How many days do you want to spend in the hotel? a

     Invalid Input! Must be a number.

 How many days do you want to spend in the hotel? 0

     Please enter a valid number.

 How many days do you want to spend in the hotel? 4

 Please choose the room from the following options:
  Enter 1 for the Deluxe Double Room. @RM550
  Enter 2 for the Double Room.        @RM400
  Enter 3 for the Standard Room.      @RM300
   Enter choice: a

     Invalid Input! Must be a number.

 Please choose the room from the following options:
  Enter 1 for the Deluxe Double Room. @RM550
  Enter 2 for the Double Room.        @RM400
  Enter 3 for the Standard Room.      @RM300
   Enter choice: 0

     Please enter a valid number.

 Please choose the room from the following options:
  Enter 1 for the Deluxe Double Room. @RM550
  Enter 2 for the Double Room.        @RM400
  Enter 3 for the Standard Room.      @RM300
   Enter choice: 6

     Please enter a valid number.

 Please choose the room from the following options:
  Enter 1 for the Deluxe Double Room. @RM550
  Enter 2 for the Double Room.        @RM400
  Enter 3 for the Standard Room.      @RM300
   Enter choice: 2

 Enter 'Y' for yes or 'N' for no for the following optional services:

 Do you want a laundry service during your stay? (RM20): y

 Do you want an extra towel during your stay? (RM10): y

 Do you want transportation service during your stay? (RM50): y

 The Total charges are RM1680



 Press Enter to continue:



See what you think of it.

Andy
If you use functions for input, this greatly simplifies the program. Consider:

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
#include <iostream>
#include <string>
#include <limits>
#include <cctype>
#include <type_traits>
#include <typeinfo>

template<typename T = int>
typename std::enable_if_t< std::is_integral_v<T>, T>
getInp(const std::string& prm)
{
	const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
	T n {};

	while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
		std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return n;
}

char getYesNo(const std::string& prm)
{
	char ch {};

	do {
		ch = static_cast<char>(std::toupper(static_cast<unsigned char>(getInp<char>(prm))));
	} while ((ch != 'N' && ch != 'Y') && (std::cout << "Invalid option\n"));

	return ch;
}

int main()
{
	constexpr unsigned Perday[] {550, 400, 300};
	constexpr unsigned laundayCost {20};
	constexpr unsigned extratowelCost {10};
	constexpr unsigned transportCost {50};

	int days {};
	int roomType {};
	unsigned extras {};

	do {
		days = getInp("How many days do you want to spend in the hotel: ");
	} while (days < 1 && (std::cout << "Must spend at least 1 day!\n"));

	std::cout <<
		"\nPlease choose the room from the following options:\n" <<
		"Enter 1 for the Deluxe Double Room. @RM" << Perday[0] << '\n' <<
		"Enter 2 for the Double Room. @RM" << Perday[1] << '\n' <<
		"Enter 3 for the Standard Room. @RM" << Perday[2] << "\n\n";

	do {
		roomType = getInp("Enter room type (1 - 3): ");
	} while ((roomType < 1 || roomType > 3) && (std::cout << "Invalid room type\n"));

	std::cout << "\nEnter 'Y' for yes or 'N' for no for the following optional services:\n";

	if (getYesNo("\nDo you want a laundry service during your stay? (RM" + std::to_string(laundayCost) + ") (Y/N): ") == 'Y')
		extras += laundayCost;

	if (getYesNo("Do you want an extra towel during your stay? (RM" + std::to_string(extratowelCost) + ") (Y/N): ") == 'Y')
		extras += extratowelCost;

	if (getYesNo("Do you want transportation service during your stay? (RM" + std::to_string(transportCost) + ") (Y/N): ") == 'Y')
		extras += transportCost;

	const auto baseCharges {days * Perday[roomType - 1]};
	const auto serviceCharges {days * extras};

	std::cout << "\nThe Total charges are RM" << baseCharges + serviceCharges << '\n';
}

Last edited on
Topic archived. No new replies allowed.