output of funtion

Hi

I have what I thought to be an easy function in a queue simulation.
Since it is from the textbook I expect it to work but I am getting odd output.

The Function:
int serverListType::getNumberOfBusyServers()
{
int busyServers = 0;
//int i;
cout<<"168 busy severs is "<<busyServers<<endl;
for(int i = 0; i < numOfServers; i++){
if(!servers[i].isFree()){
busyServers++;
cout<<"171 busy severs is "<<busyServers<<endl;
}
}
numBusyServer = busyServers;
return busyServers;

}

then in main

numberBusyServers = sList.getNumberOfBusyServers();
cout <<"94 main num busy servers is "<< cout <<numberBusyServers <<endl;
I expect the number outputed to the screen to be the same from the function and in main but I get :
168 busy severs is 0
94 main num busy servers is 0x6fcc92e4
96 main num busy servers is 0x6fcc92e40

the zero is within the function but I am getting a memory location in main.
I'm having a hard time testing the int in main because of this.

thanks
What is the type of numberBusyServers ?
it is an int.
int numberBusyServers =0;

I decided to try and compile and run this under linux rather than on my windows laptop.

now I get the error
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

At least it's giving me a run time error, but I'm still not sure why.
Last edited on
mistake of my part, not initialising an int.
It does the same thing.
Please provide a compilable, minimal example that still has the problem.
It's strange I wonder if I set something wrong in my compiler, though I didn't know you could, cause now it happens with all int.

#include <cstdlib>
#include<iostream>

using namespace std;

/*
*
*/
int main(int argc, char** argv) {
//int numOfServers =1;
int numBusyServer =0;

cout <<"35 num busy servers is @"<< cout <<numBusyServer<<"@"<<endl;

numBusyServer =6;
cout <<"38 num busy servers is @"<< cout <<numBusyServer<<"@"<<endl;
if( numBusyServer > 1){
cout <<"26 num busy servers is @"<< cout <<numBusyServer<<"@"<<endl;
}
if( numBusyServer !=6){
cout <<"29 num busy servers is @"<< cout <<numBusyServer<<"@"<<endl;
}

return 0;
}

output is:
35 num busy servers is @0x804a0440@
38 num busy servers is @0x804a0446@
26 num busy servers is @0x804a0446@

strange the last character in the output is the int.
but if fails the compare test != 6

That's just because you're trying to print the cout object. That obviously doesn't go down well.
dooh now to figure out the real bug.
Topic archived. No new replies allowed.