[英]Is there a way to take user input to define array size in Visual Studio?
我試圖獲取用戶輸入n
( cin >> n
)並使用該n
定義數組大小( arr[n]
)。 它可以在其他編譯器上工作,但是在Visual Studio上卻無法工作,因此嘗試查找問題的根源,是否有解決辦法?
這是一個hackerrank問題,我的提交已被成功接受,我已經在多個在線和脫機編譯器上進行了嘗試,我的代碼有效,僅在Visual Studio上遇到了此問題。
int n;
cin >> n;
int arr[n]; //this line has the error
我得到的錯誤是:
錯誤(有效)E0028表達式在第9行中必須具有恆定值Project2 D:\\ Code \\ C ++ \\ source \\ Project2 \\ Source.cpp
為此,您可以按以下方式使用動態1D數組。
int count;
cin >> count;
int *a = new int[count];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.