2015-10-04 92 views
0

我想在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的命令行復制:

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

我使用的是最新版本的默認編譯器設置代碼塊的。謝謝。

回答

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

你有兩種cout一條線,因爲沒有上線別無分號1

爲了解決這個問題無論是擺脫第二cout或在第一行的每個COUT末尾加上一個分號聲明。

如果你看看每個答案的最後2位數字,你會看到你想要得到的答案,所以它仍然打印出你想要的答案,只是指針指向cout

+0

有趣。我從來沒有做過「cout」,並且從來沒有預料到它會打印出這樣的結果。 –

+0

哈哈!非常感謝,男士!我不知道我怎麼沒看到:s –

+1

奇怪的十六進制數的解釋可以在http://stackoverflow.com/questions/10987156/does-stdcout-have-a-return-value – Ritesh