Link to home
Start Free TrialLog in
Avatar of KazIT
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(businessEntries, economyEntries)) return;
my program doesn't give me the required cout statement if the conditions are true.

Kaz

Avatar of Axter
Axter
Flag of United States of America image

Please post code calling this function with the true condition.

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.
you can put a

cout << businessEntries << economyEntires;

before the if ()
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KazIT
KazIT

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(businessEntries, 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.
Avatar of KazIT

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of KazIT

ASKER

Thanks all for the help.  :)