奇怪的十六进制回答了code :: blocks中的简单数学

时间:2015-10-04 23:41:56

标签: c++ codeblocks

我试图在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

我使用最新版本的代码块和默认的编译器设置。感谢。

1 个答案:

答案 0 :(得分:3)

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

由于第1行没有分号,因此一行中有两个cout

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

如果你查看每个答案的最后两位数字,你会看到你希望得到的答案,所以它仍然会在指针cout之后打印出你想要的答案。