Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

function not working when called.

Posted on 2004-08-04
8
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1600 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

636 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