Solved

function not working when called.

Posted on 2004-08-04
8
194 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now