2013-05-10 53 views
0

初始化在C#一個字符串,因爲這很容易:如何使用用戶輸入初始化* char?

string str = Console.Read(); 

用這種方法,我不需要知道哪些用戶輸入的字符串的大小。但我在C++中找不到像這樣的方式。我希望我的字符串被定義爲char *input,,我不想知道字符串的大小。

我怎樣才能達到我想要的?

+0

顯示你已經嘗試過的東西。 – 2013-05-10 14:11:28

+2

使用'std :: string'。不需要'char *' – stardust 2013-05-10 14:13:10

+0

在C#中,你想要使用字符串,但在C++中,你不想使用std :: string,這是使用字符串的首選方式。在這種情況下,代碼將是'std :: string stringInput; getline(std :: cin,stringInput);'。 – stefaanv 2013-05-10 14:17:52

回答

5

爲什麼不使用C++的string類型?

#include <iostream> 
#include <string> 

int main() { 
    std::string foo; 
    std::cin >> foo; 
    std::cout << foo << "\n"; 
} 
1

C++有一個類似C#的字符串的字符串類。所以使用它。 :)

char*不是字符串。如果你在C工作,它就是最接近的。

因此,#include <string>,然後使用std::string而不是char*

相關問題