[英]c++ application crashes when no args are entered
我正在創建一個基於命令行的加密軟件。 我在這里有一個if語句,以檢查是否沒有參數,並以代碼1退出。
if (argc = 0) {
errorExit("No arguments listed. Type \"-h\" for help.", 1);
}
errorExit()在我創建的頭文件中。
int errorExit(string errMsg, int errorCode) {
cerr << "Error: ";
cerr << errMsg;
cerr << endl;
exit(errorCode);
return NULL;
}
當我不輸入任何參數時,我的程序就會崩潰。
Z:\Code\C++\CodeBlocks\simplecrypt\bin\Debug>simplecrypt.exe
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Z:\Code\C++\CodeBlocks\simplecrypt\bin\Debug>
為了消除混亂,我在mingw中使用了代碼塊,以防mingw中的錯誤。
編輯:我意識到我有argc = 0而不是argc ==0。我意識到了錯誤,但仍然沒有解決問題。
主要函數可以這樣聲明:
int main (int argc, char *argv[]);
當您在命令行中調用程序時,argv [0]將是可執行文件的名稱,這意味着如果沒有其他參數,則argc應該為1。
if(argc == 0){...
在if條件中寫==和not =
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.