【问题标题】:Weird hexadecimal answers to simple math in code::blockscode::blocks 中简单数学的奇怪十六进制答案
【发布时间】:2015-10-04 23:41:55
【问题描述】:

我正在尝试在 C++ 书中完成以下任务。

运行后:

#include <iostream>

using namespace std;

int main()
{
double first_arg;
double second_arg;

cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;

cout << first_arg << " * " << second_arg <<  " = "
     << cout << first_arg * second_arg << "\n";

cout << first_arg << " + " << second_arg << " = "
     << cout << first_arg + second_arg << "\n";

cout << first_arg << " / " << second_arg << " = "
     << cout << first_arg / second_arg << "\n";

cout << first_arg << " - " << second_arg << " = "
     << cout << first_arg - second_arg << "\n";

我得到了一些意想不到的结果。就像直接从 windows cli 复制的结果一样:

Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2

我正在使用具有默认编译器设置的最新版本的代码块。谢谢。

【问题讨论】:

    标签: c++ codeblocks


    【解决方案1】:
    cout << first_arg << " * " << second_arg <<  " = "
         << cout << first_arg * second_arg << "\n";
    

    一行中有两个cout,因为第一行没有分号

    要解决此问题,要么删除第二个 cout,要么在每个 cout 语句的第一行末尾添加一个分号。

    如果您查看每个答案的最后 2 位数字,您将看到您希望得到的答案,因此它仍然会在指向 cout 的指针之后打印出您想要的答案。

    【讨论】:

    • 有趣。我从未制作过cout &lt;&lt; cout;,也从未想过它会打印出这样的结果。
    • 哈哈!非常感谢,伙计!我不知道我怎么没看到:s
    • 奇怪的十六进制数的解释可以在stackoverflow.com/questions/10987156/…找到
    • cout 没有指向任何东西,它只是在旧版本的 C++ 中不幸地转换为 void*
    • @chris 是的,我知道我被误导了。我认为删除我的答案不是一个好的选择,但是因为它确实解决了他的问题,所以我只是编辑了我的答案以摆脱它。如果有人想知道它为什么输出这个,Ritesh 提供了一个很好的阅读链接
    猜你喜欢
    • 2018-04-14
    • 2014-02-12
    • 2013-12-08
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多