2013-05-06 121 views
-1

我是C++新手,嘗試打印'Hello world'。C++輸出數字而不是字符串

#include <iostream> 

using namespace std; 

int main() { 
    cout << 'Hello world!'; 
    return 0; 
} 

但是結果我得到'1919706145'。我做錯了什麼?

+0

有關單引號和雙引號的更多信息:http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters – Atle 2013-05-06 13:12:06

+2

這是一個非常好的主意,以啓用編譯器警告。這至少應該告訴你某件事情並不在這裏,儘管一開始可能很難弄清楚它的含義。 – 2013-05-06 13:12:51

回答

10

字符串由代表」,而不是「

#include <iostream> 

using namespace std; 

int main() { 
    cout << "Hello world!"; // Use " not ' 
    return 0; 
} 
+0

謝謝。我曾經使用這兩種類型的引號) – 2013-05-06 13:33:20

2

嘗試這樣做:

cout << "Hello world!"; // <---------Double Quotes 

字符串使用雙引號單引號是單個字符

3

您應該使用:

cout << "Hello World!" << endl; 

對字符而不是字符串使用''。 字符是'h','i'等單個字母,而字符串是「hi」。