2015-11-07 49 views
-3

問題是什麼?「美化」/評論問題代碼

如果在其他語句和else語句不起作用的情況下運行if語句,則返回3。

你是什麼意思「它不工作」?

int handleArgs(int argc, char *argv[]) { 
    int x = 0; 
    if (argc <= 1) { 
     return x; 
    } 

    x = 1; 

    for (int i = 1; i < argc; i++) { 

     string argument = argv[i];         // Convert argument to string 

     if (argument=="-v" || argument=="--version") {     //*** If user asks for version info ***\\ 
      cout << "\nYou are running \n Dev0.0.0" << endl; 

     } else if (argument == "-a " || argument == "--admin") { //*** If user tries to open console ***\\ 
      cout << "\nSorry, console feature currently unavailable" << endl; 

     } else if (argument=="-h" || argument=="--help") {   //*** If user asks for help ***\\ 

      cout << "\nUsage: " << argv[0] << " -[switch] argument(s)\n"; 
      cout << " -a, --admin  Open console view. Requires password\n"; 
      cout << " -v, --version  Print version and exit\n"; 
      cout << " -h, --help   Print this message and exit\n" << endl; 

     } else { 
      cout << " Is you dumb?\nIncorrect syntax/argument(s)\n '" <<  argv[0] << " --help' for help." << endl; 
     } 
    } 
} 
     return x; 

} 具體來說,if(condition)從不執行,即使條件爲真。

第一個else if(condition)也不會執行。

如果爲true,則執行第二個else if語句。

else如果是別的時候真實,

  1. 如果聲明是真實的執行。
  2. 1st else if語句爲true。
  3. 這些陳述都不是真實的。
  4. 如果2nd else if語句爲true,則不適用。

摘要

基本上,它就像if和第一else if不存在。

編輯:如果Else語句不工作C++

+2

粘貼源代碼在這裏。 –

+0

@BenVoigt替換鏈接? – Gabe

+0

是的,就像MikeCAT一樣。鏈接可能導致病毒和東西,短代碼片斷要好得多。 –

回答

0

您的評論未被正確終止。以下作品:

int handleArgs(int argc, char **argv) 
{ 
int x=0; 
if (argc <= 1) {return x;} 
x = 1; 

for (int i=1; i < argc; i++) 
{ 
    string argument(argv[i]);         // Convert argument to string 

    if (argument==std::string("-v") || argument=="--version") 
    { 
     cout << "\nYou are running \n Dev0.0.0" << endl; 
    } 
    else if (argument == "-a " || argument == "--admin") 
    { 
     cout << "\nSorry, console feature currently unavailable" << endl; 
    } 
    else if (argument=="-h" || argument=="--help") 
    {   
     cout << "\nUsage: " << argv[0] << " -[switch] argument(s)\n"; 
     cout << " -a, --admin  Open console view. Requires password\n"; 
     cout << " -v, --version  Print version and exit\n"; 
     cout << " -h, --help   Print this message and exit\n" << endl; 
    } 
    else 
    { 
    cout << " Is you dumb?\nIncorrect syntax/argument(s)\n '" <<  argv[0] << " --help' for help." << endl; 
    } 
} 
+0

Thx,猜我不應該'美化'我的代碼。剛剛給我造成了問題 – Gabe

1

如果你行的末尾寫\標題已經從」改變,這意味着未來的行將通過預處理器連接起來。然後,將刪除包含cout的「聲明」的評論。

刪除這些垃圾\ s並整齊地格式化您的代碼。

#include <iostream> 
#include <string> 
using std::cin; 
using std::cout; 
using std::endl; 
using std::string; 

int handleArgs(int argc, char *argv[]) { 
    int x=0; 
    if (argc <= 1) { 
     return x; 
    } 
    x = 1; 

    for (int i=1; i < argc; i++) { 
     string argument = argv[i];         // Convert argument to string 

     if (argument=="-v" || argument=="--version") {    //*** If user asks for version info *** 
      cout << "\nYou are running \n Dev0.0.0" << endl; 
     } else if (argument == "-a " || argument == "--admin") { //*** If user tries to open console *** 
      cout << "\nSorry, console feature currently unavailable" << endl; 
     } else if (argument=="-h" || argument=="--help") {   //*** If user asks for help *** 
      cout << "\nUsage: " << argv[0] << " -[switch] argument(s)\n"; 
      cout << " -a, --admin  Open console view. Requires password\n"; 
      cout << " -v, --version  Print version and exit\n"; 
      cout << " -h, --help   Print this message and exit\n" << endl; 
     } else { 
      cout << " Is you dumb?\nIncorrect syntax/argument(s)\n '" <<  argv[0] << " --help' for help." << endl; 
     } 
    } 
    return x; 
} 

int main(int argc, char *argv[]) { 
    return handleArgs(argc, argv); 
} 

注:不應該"-a ""-a"(刪除空格)?

+0

Thx,猜我不應該'美化'我的代碼。只是導致問題 – Gabe