KazIT
asked on
function not working when called.
Hi,
I have a function
bool checkEmpty(int businessEntries, int economyEntries)
{
if(businessEntries ==0 && economyEntries == 0)
{
cout <<"The reservations are empty. No action taken!\n";
return true;
}
return false;
}
but when I call it within other functions by
if (checkEmpty(businessEntrie s, economyEntries)) return;
my program doesn't give me the required cout statement if the conditions are true.
Kaz
I have a function
bool checkEmpty(int businessEntries, int economyEntries)
{
if(businessEntries ==0 && economyEntries == 0)
{
cout <<"The reservations are empty. No action taken!\n";
return true;
}
return false;
}
but when I call it within other functions by
if (checkEmpty(businessEntrie
my program doesn't give me the required cout statement if the conditions are true.
Kaz
you can put a
cout << businessEntries << economyEntires;
before the if ()
cout << businessEntries << economyEntires;
before the if ()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Axter,
This is one of the functions that uses checkEmpty.
void cancelBooking(string name[], long phoneNo[],string seatClass[],
int& businessEntries,int& economyEntries)
{
if (checkEmpty(businessEntrie s, economyEntries)) return;
cout<<"Please enter the name to be cancelled: ";
cin>>ws;
string toDelete = getValidName();
// find the booking in the reservations system
int index =-1;
index = seqSearch(name,toDelete, businessEntries, economyEntries);
if(index == -1)
{
cout<< toDelete << " is not in the booking list.\n"<<endl;
return;
}
cout<< toDelete << " booking cancelled.\n"<<endl;
// blank out the deleted name
name[index] = "";
}
if there have been no bookings entered at all it doesn't say "The Reservations are empty"........
It does however, search for any name you enter and return the result.
This is one of the functions that uses checkEmpty.
void cancelBooking(string name[], long phoneNo[],string seatClass[],
int& businessEntries,int& economyEntries)
{
if (checkEmpty(businessEntrie
cout<<"Please enter the name to be cancelled: ";
cin>>ws;
string toDelete = getValidName();
// find the booking in the reservations system
int index =-1;
index = seqSearch(name,toDelete, businessEntries, economyEntries);
if(index == -1)
{
cout<< toDelete << " is not in the booking list.\n"<<endl;
return;
}
cout<< toDelete << " booking cancelled.\n"<<endl;
// blank out the deleted name
name[index] = "";
}
if there have been no bookings entered at all it doesn't say "The Reservations are empty"........
It does however, search for any name you enter and return the result.
ASKER
Axter,
I have run the debug putting a breakpoint on the if statement. It seems the variables have a value of businessEntries=1 and economyEntries=9. Am I right to assume that it's picking up initialized values from the main function?
int main(void)
{
string name[MAX_ENTRIES] = {" "}, seatClass[MAX_ENTRIES]={" "} ;
long phoneNo[MAX_ENTRIES] = {0};
int businessEntries = 0, economyEntries = 3 * 3; // Economy skips the first 3 rows and 3 columns
and if so does that mean I have to somehow initialize the variables within the checkEmpty function?
I have run the debug putting a breakpoint on the if statement. It seems the variables have a value of businessEntries=1 and economyEntries=9. Am I right to assume that it's picking up initialized values from the main function?
int main(void)
{
string name[MAX_ENTRIES] = {" "}, seatClass[MAX_ENTRIES]={" "} ;
long phoneNo[MAX_ENTRIES] = {0};
int businessEntries = 0, economyEntries = 3 * 3; // Economy skips the first 3 rows and 3 columns
and if so does that mean I have to somehow initialize the variables within the checkEmpty function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Another tiny suggestion...
cout << "The reservations are empty" << endl;
You've ended the string with a \n, which would print the line if you were using printf(), but it isn't guaranteed to do the same thing using C++ I/O. You could be getting the message but not seeing it because the stream buffer wasn't flushed to the output.
I've seem implementations that work both ways. std::endl will definitely flush the buffer.
cout << "The reservations are empty" << endl;
You've ended the string with a \n, which would print the line if you were using printf(), but it isn't guaranteed to do the same thing using C++ I/O. You could be getting the message but not seeing it because the stream buffer wasn't flushed to the output.
I've seem implementations that work both ways. std::endl will definitely flush the buffer.
ASKER
Thanks all for the help. :)
The problem more then likely is that you're really not getting a true condition.
Try putting a break point on the if condition, and then examin the variable values to verify their condition.