2010-10-29 101 views
2

我是C++的新手,但實際上我正在學習實驗部分,但在試驗時遇到了cout函數的問題。編譯時程序失敗。我想知道你們是否可以幫助我:這是我寫的來源。C++源代碼幫助,cout函數幫助

#include "stdafx.h" 
#include <iostream> 
using namespace std; 

int main() 
{ 
signed short int a; 
signed short int b; 
signed short int c; 
a = 25; 
b = 8; 
c = 12; 

cout << a << endl; 
cout << b << endl; 
cout << c << endl; 
cout << "What is the sum of a + b - c? The answer is: "; 
cout << a + b - c; 
cout << endl; 
cout << "Why is this?" << endl; 
cout << "This is because: "; 
cout << "a + b equals: " << a + b << endl; 
cout << "and that minus " c << " is" << a + b - c << endl; 
cout << "If that makes sense, then press enter to end the program."; 

cin.get(); 
return 0; 


} 

我也想知道什麼是有符號和無符號的意思,我認爲它是依賴於編譯器?我正在使用Visual C++ 2008速成版。

感謝任何能指出我的錯誤並幫助我的人!

+5

您可能希望添加所看到的確切錯誤消息,並指出它所指示的行。 (並非我們都是編譯器。) – sbi 2010-10-29 20:15:49

+0

通常'signed'被省略,因爲這是正常情況,只是假設。它與'auto'類似,而且很少使用,所以下一個C++標準使它完全不同。 – 2010-10-29 20:30:28

回答

10
cout << "and that minus " c << " is" << a + b - c << endl; 
//      ^

您錯過了<<


unsigned意味着數據類型只能存儲非負整數,而signed意味着它可以存儲負整數,以及(如在其可具有負「標誌」)。

支持的整數的確切範圍是平臺相關的。通常情況下,一個unsigned short支持範圍爲0值65535和signed short支持-32768到32767

2
cout << "and that minus " c << " is" << a + b - c << endl; 

你錯過字符串「和減」和C短之間的「< <」。

簽名意味着一位專門用於確定該值是否爲負數,但這也意味着您無法像使用無符號數一樣大。默認情況下,除非您另行指定,否則變量將被簽名