### 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).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205`` ``````// Program description: This program will calculate the cost of a guest staying at a hotel. // ******************************************************************************************* #include #include 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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````// Program description: This program will calculate the cost of a guest staying at a hotel. #include #include 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:
 ``1234567`` ``````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.