问题'美化/评论代码

时间:2015-11-07 01:58:58

标签: c++ if-statement

问题是什么?

运行带有3个else语句的if语句和最后的else语句不起作用。

你是什么意思"它不起作用"

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执行if和

  1. if语句为真。
  2. 第一个if语句为真。
  3. 这些陈述均不属实。
  4. 如果第二个else if语句为真,则不行。
  5. 摘要

    基本上,它与if和第一个else if不存在一样。

    编辑:标题已从&#39;如果Else Statement not Working C ++

    更改

2 个答案:

答案 0 :(得分:1)

如果在行尾写\,则意味着下一行将由预处理器连接。然后,将删除包含cout的“声明”的评论。

删除这些垃圾\并整齐地格式化您的代码。

#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"(删除空格)吗?

答案 1 :(得分: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;
  }
}