Please help on ostream output error

Z

Zhixin Han

Hi,

I got a problem while using the ostream operator to print a trace
message. Normally it is printing something like this:

[Oc: 0 Rk: 1]

The code is:

os << " [Oc: " << CpName.getOcc() <<" Rk: " << CpName.getRank() <<
"]";

But I saw the following outputs, with the characters "Rk: #]" at the
end of line messed up in different manners:

Wrong output #1:
[Oc: 0 Rk:

Wrong output #2:
[Oc: 0 R 1]

Wrong output #3:
[Oc: 0 Rk2]

Wrong output #4:
[Oc: 0 : 2]

I am wondering how can this happen. Could this be caused by memory
problem? How can I prevent this from happening?

Thanks.


Zhixin
 
Z

Zhixin Han

Victor Bazarov said:
Zhixin said:
I got a problem [...]

I am wondering how can this happen. Could this be caused by memory
problem? How can I prevent this from happening?

This is described in the FAQ 5.8. You can find FAQ in the link below

http://www.parashift.com/c++-faq-lite/

Victor

Thanks. CpName::getOcc() and CpName::getRank() are defined as:

const short int getOcc() const {return _occ; }
const short int getRank() const {return _rank;}
 
V

Victor Bazarov

Zhixin said:
Victor Bazarov said:
Zhixin said:
I got a problem [...]

I am wondering how can this happen. Could this be caused by memory
problem? How can I prevent this from happening?

This is described in the FAQ 5.8. You can find FAQ in the link below

http://www.parashift.com/c++-faq-lite/

Victor


Thanks. CpName::getOcc() and CpName::getRank() are defined as:

const short int getOcc() const {return _occ; }
const short int getRank() const {return _rank;}

You probably didn't understand what the FAQ says. Do I need to explain?
You are supposed to post the _minimal_ _complete_ _compilable_ code that
still produces the error you're asking about. It is not enough to see
the output statement, two member functions and the wrong output. Do you
understand "not enough"? Post the _complete_ program with the effect
you are seeing. Then we can discuss a possible cause and solutions. We
are not mind readers here.

V
 
Z

Zhixin Han

Victor Bazarov said:
Zhixin said:
Victor Bazarov said:
Zhixin Han wrote:

I got a problem [...]

I am wondering how can this happen. Could this be caused by memory
problem? How can I prevent this from happening?

This is described in the FAQ 5.8. You can find FAQ in the link below

http://www.parashift.com/c++-faq-lite/

Victor


Thanks. CpName::getOcc() and CpName::getRank() are defined as:

const short int getOcc() const {return _occ; }
const short int getRank() const {return _rank;}

You probably didn't understand what the FAQ says. Do I need to explain?
You are supposed to post the _minimal_ _complete_ _compilable_ code that
still produces the error you're asking about. It is not enough to see
the output statement, two member functions and the wrong output. Do you
understand "not enough"? Post the _complete_ program with the effect
you are seeing. Then we can discuss a possible cause and solutions. We
are not mind readers here.

V

I understand what the FAQ says. I just thought the code I put in the
message is clear "enough" for the question. Maybe not enough according
to the group's standard. Then I am sorry for that.

The code compiles and works fine, except one time recently I saw the
wrong outputs. So it is not that the code can't compile or has an
implicit error. I am just wondering how this can happen.

Thanks for your inputs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top