C++ Code Error

On line 69 I receive an error saying that total is not declared in this scope, I am unsure what I did wrong.

// 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;
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, total, memberDiscount);

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(bCost, rCost, 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 membership()
{
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 memberDiscount, float total)
{
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;
}
in "discountCost = mCost(rCost, nights, total, memberDiscount);" total was not declared.
After that I got that your "char getMembership()" is not named properly at the end of the code, it just say "char membership()"
Last edited on
1) Please use code tags when posting code, to make it readable:

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

2) In your main() function, you're attempting to use a variable called total, in your call to mCost(). But you haven't declared that variable anywhere in main().
Topic archived. No new replies allowed.