Value not coming out correctly in .txt file

I'm unsure what I did wrong, but whenever I run my program the value for mCost comes out incorrectly, I'm trying to figure out what I did wrong (This doesn't happen when I run the program to calculate the nCost).

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Program description: This program will calculate the cost of a guest staying at a hotel.
// *******************************************************************************************
#include <iostream>
#include <fstream>
using namespace std; 

float mCost(float, float, float, float);
float nCost(float, float, float);
char getMembership();
float calcDiscount(float, float);

int main()
{
	int nights, room, rCost, memberType, buffet, bCost, total;
	char isMember = getMembership();
	float discountCost;
	float memberDiscount;
	
	if(isMember == 'M' || isMember == 'm')
	{
	do{
		cout << "Please enter number of nights stayed at hotel." << endl;
		cin >> nights;  
	
		cout << "Please select type of room used, 1 = Economy Room, 2 = Standard Room, 3 = Luxury Room." << endl;
		cin >> room;
	
	if(room == 1)
	{
		rCost = 70;
	}
	
	if(room == 2)
	{
		rCost = 100;
	}
	
	if(room == 3)
	{
		rCost = 150;
	}
	
		cout << "Please enter type of membership used, 1 = Gold, 2 = Bronze, 3 = Silver." << endl;
		cin >> memberType;
		
	if(memberType == 1)
	{
		memberDiscount = .150;
	}
	
	if(memberType == 2)
	{
		memberDiscount = .100;
	}
	
	if(memberType == 3)
	{
		memberDiscount = .050;
	}
	}
	while(nights == 0 || room == 0);
	}
	
    discountCost = mCost(rCost, nights, memberDiscount, total);
	
		if(isMember == 'N' || isMember == 'n')
	{
	do{
		cout << "Please enter number of nights stayed at hotel." << endl;
		cin >> nights;  
	
		cout << "Please select type of room used, 1 = Economy Room, 2 = Standard Room, 3 = Luxury Room." << endl;
		cin >> room;
	
	if(room == 1)
	{
		rCost = 70;
	}
	
	if(room == 2)
	{
		rCost = 100;
	}
	
	if(room == 3)
	{
		rCost = 150;
	}
	
		cout << "Please select if you ate at the breakfast buffet while staying at hotel, 1 = Yes, 2 = No." << endl;
		cin >> buffet;
		
	if(buffet == 1)
	{
		bCost = 10;
	}
	if(buffet == 2)
	{
		bCost = 0;
	}
	}
	while(nights == 0 || room == 0);
	}
	
	float nTotalCost = nCost(rCost, bCost, nights);
	
	 ofstream myfile ("HotelInvoice.txt");
  if (myfile.is_open())
  {
    myfile << "This is the total member cost: $" << discountCost << endl;
	myfile << "This is the total non-member cost: $" << nTotalCost << endl;
    myfile.close();
  }
  else cout << "Unable to open file";
	
	return 0;
}

//**********************************************************************************
// void membership 
// this function returns a char to show whether a customer is a member or not
//
// return value
// ------------------
// char
//
// Parameters
// ------------------
// *********************************************************************************


char getMembership()
{
	char letter;
	
	cout << "Enter your choice (M = Member or N = Non-member): " << endl;
	cin >> letter;
	
	while(letter != 'M' && letter != 'm' && letter != 'N' && letter != 'n')
	{
		cout << "Please enter M or N: " << endl;
		cin >> letter;
		
		return letter; 
	}
}

//****************************************************************
// void mCost*
// This function calculates the cost of stay for members
//
// return value
// ---------------
// float
//
// Parameters
// ---------------
// float rCost, float nights
//****************************************************************

float mCost(float rCost, float nights, float total, float memberDiscount)
{
	float mTotal = rCost*nights;
	float dTotal = calcDiscount(total, memberDiscount);
	return dTotal;
}

//****************************************************************
// void calcDiscount
// This function calculates the discount for members
// 
// return value
// ---------------
// float
//
// Parameters 
// ---------------
// float total, float memberDiscount
//****************************************************************

float calcDiscount(float total, float memberDiscount)
{
	float discount = total*memberDiscount;
	float discountTotal = total-discount;
	return discountTotal;
}

//****************************************************************
// void nCost
// This function calculates the cost of stay for non-members
// 
// return value
// ---------------
// float
// 
// Parameters
// ---------------
// float rCost, float bCost, float nights
//****************************************************************

float nCost(float rCost, float bCost, float nights)
{
	float total = (bCost*nights)+(rCost*nights);
	return total;
}
Last edited on
I fixed your program. I also made it a bit cleaner. I took all of your functions and put them at the top, while turning them into single return statements without unnecessary variables:
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
// Program description: This program will calculate the cost of a guest staying at a hotel.
#include <iostream>
#include <fstream>
using namespace std; //In the future, use std:: instead of this line.

float calcDiscount(float total, float memberDiscount) { return total-(total*memberDiscount); }
float mCost(float rCost, float nights, float memberDiscount, float total) { return calcDiscount(rCost*nights, memberDiscount); }
float nCost(float rCost, float bCost, float nights) { return (bCost*nights)+(rCost*nights); }

char getMembership()
{
	char letter;
	
	cout << "Enter your choice (M = Member or N = Non-member): " << endl;
	cin >> letter;
	
	while(letter != 'M' && letter != 'm' && letter != 'N' && letter != 'n')
	{
		cout << "Please enter M or N: " << endl;
		cin >> letter;
		
	}
	return letter;
}

/* instead of declaring functions, simply put them above main */

int main()
{
	int nights{}, room{}, rCost{}, memberType{}, buffet{}, bCost{}, total{}; //you should initialize all these variables.
	char isMember = getMembership();
	float discountCost{}, memberDiscount{};
	
	if(isMember == 'M' || isMember == 'm')	{  
	    
	    do {
		    cout << "Please enter number of nights stayed at hotel: ";
		    cin >> nights;  
		    cout << endl;
	
		    cout << "Please select type of room used, 1 = Economy Room, 2 = Standard Room, 3 = Luxury Room: ";
		    cin >> room;
		    cout << endl;
	        if(room == 1) rCost = 70;
	        if(room == 2) rCost = 100;
	        if(room == 3) rCost = 150;
	        
		    cout << "Please enter type of membership used, 1 = Gold, 2 = Bronze, 3 = Silver: " << endl;
		    cin >> memberType;
	        if(memberType == 1) memberDiscount = .150;
	        if(memberType == 2) memberDiscount = .100;
	        if(memberType == 3) memberDiscount =.050;
	        //Instead of all these if statements, you could simply use a switch statement

	    } while(nights == 0 || room == 0);
	 }
	
    discountCost = mCost(rCost, nights, memberDiscount, total);
	
		if(isMember == 'N' || isMember == 'n') {
		    
	        do {
	            
		        cout << "Please enter number of nights stayed at hotel." << endl;
		        cin >> nights;  
	
		        cout << "Please select type of room used, 1 = Economy Room, 2 = Standard Room, 3 = Luxury Room: ";
		        cin >> room;
		        cout << endl;
	            if(room == 1) rCost = 70;
	            if(room == 2) rCost = 100;
	            if(room == 3) rCost = 150;
	            
		        cout << "Please select if you ate at the breakfast buffet while staying at hotel, 1 = Yes, 2 = No." << endl;
		        cin >> buffet;
	            if(buffet == 1) bCost = 10;
	            if(buffet == 2) bCost = 0;
	            
	        } while(nights == 0 || room == 0);
        }
	
	float nTotalCost = nCost(rCost, bCost, nights);
	
  //ofstream myfile ("HotelInvoice.txt");
  //if (myfile.is_open()) {
    cout << "This is the total member cost: $" << discountCost << endl;
	cout << "This is the total non-member cost: $" << nTotalCost << endl;
    //myfile.close();
  //}
  //else cout << "Unable to open file";
	
	return 0;
}


Ok, the first problem I see is this:
1
2
3
4
5
6
7
while(letter != 'M' && letter != 'm' && letter != 'N' && letter != 'n')
	{
		cout << "Please enter M or N: " << endl;
		cin >> letter;
		
		return letter; 
	}


the return statement should be outside the that final brace. This caused the program to not run correctly.

Secondly,
 
int nights, room, rCost, memberType, buffet, bCost, total;


you never use the total variable at all. As in, you never define it. Therefore, it's value is unknown. This is what's causing your program to give you random numbers when it outputs, because you never initialized the variable when you created it, so it just spits out whatever value is located at that memory address.

Also, when you called the mCost function in your original code, you got the order of the parameters wrong, which is why it didn't give you the correct number (because the calculation was wrong). A better way to have written this is using classes.
discountCost = mCost(rCost, nights, memberDiscount, total);
The last two arguments should be switched, because the order in your function definition is listed as such.

Good luck!

Last edited on
Topic archived. No new replies allowed.