开发者

c++ cout with float producing strange results

Currently I have the following:

float some_function(){
    float percentage = 100;
    std::cout << "percentage = " << percentage;

    //more code
    return 0;
}

which gives the output

percentag开发者_开发技巧e = 100

However when I add some std::endl like so:

float some_function(){
    float percentage = 100;
    std::cout << "percentage = " << percentage  << std::endl;

    //more code
    return 0;
}

This gives the output:

percentage = 1000x6580a8

Adding more endl's just prints out more 0x6580a8's.

What could be causing this? This is compiled with gcc 4.4.3 on Ubuntu 10.04.


The function is written correctly. On my machine ( g++ 4.4.3 on Ubuntu 10.04 ) everything works smoothly. Are you sure that the error isn't caused by some other part of the code ?


Your code is perfectly valid. I suspect that you could be smashing your stack or heap in some other part of your code as the most likely cause. 0x6580a8 is too short to be an object address. Also, he would never get the same address in two runs of the same program.


What if you tried \n ?

std::cout << "percentage = " << percentage  << "\n";


Is this your actual code, or is there a different sort of stream instead of cout?

It's taking the address of the endl manipulator instead of applying it to the stream, which implies that it can't see the matching version of endl for the stream type you're using.

What happens if you use << "\n" << std::flush instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜