Solved

function not working when called.

Posted on 2004-08-04
8
195 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

929 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

11 Experts available now in Live!

Get 1:1 Help Now