0

I am sure this question should be an easy answer, but I have been stumped for a while now. In my code, I have the main function: auto-generated, with the arguments as parameters. Then, I process the arguments and check if flags should be enabled or not. However, when I send a flag file -d or file -s, it throws a Segmentation fault: 11 (note that the text in cout gets printed). The code seems simple, thanks for any advice.

int main(int argc, const char * argv[]) {
    srand(time(0));
    bool d_on = false;
    bool s_on = false;
    
    if (argc > 1) {
        for (int i = 1; i <= argc; i++) {
            string arg = argv[i];
            
            if ((arg == "-h") || (arg == "--help")) {
                printHelpExit();
            } else if ((arg == "-d") || (arg == "--debug")) {
                cout << "DEBUG ON" << endl;
                d_on = true;
            } else if ((arg == "-s") || (arg == "-solver")) {
                cout << "SOLVER ON" << endl;
                s_on = true;
            } else {
                cout << "Invalid argument: " << arg << endl;
                printHelpExit();
            }
        }
    }

    return 0;
cold10
  • 130
  • 8

0 Answers0