Solved

function not working when called.

Posted on 2004-08-04
8
196 Views
Last Modified: 2010-04-01
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

0
Comment
Question by:KazIT
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 11723125
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11723131
you can put a

cout << businessEntries << economyEntires;

before the if ()
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 11723168
>>my program doesn't give me the required cout statement if the conditions are true.

You should also check to see if your code is not returning before it even reaches the if condition that calls checkEmpty
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:KazIT
ID: 11723217
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.
0
 

Author Comment

by:KazIT
ID: 11723244
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?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 400 total points
ID: 11723616
>> int  businessEntries = 0, economyEntries = 3 * 3;

Because of that, the checkEmpty() conditions never are true. Change it to:


bool checkEmpty(int businessEntries, int economyEntries)
{
     if(businessEntries ==0 && economyEntries == 9)
     {
          cout <<"The reservations are empty.  No action taken!\n";
          return true;
     }
     return false;
}

However, i don't know why businessEntries has a value of 1?

And if you get gaps after cancelling some reservations, businessEntries and economyEntries do not really 'count' filled seats but the next never filled seat no that is empty. I would suggest that you hold two more integers to count the number of filled seats, e. g. businessCount and economyCount.

Regards, Alex

0
 
LVL 2

Expert Comment

by:guntherothk
ID: 11725727
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.
0
 

Author Comment

by:KazIT
ID: 11741122
Thanks all for the help.  :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question