Hello. I need to determine the arguments of the command line and based on the received commands to do certain actions. Running the program out of the parameters with different parameters I get the message Segmentation fault . Help me understand what I'm doing wrong. I apologize in advance for such a terrible code.
int main(int argc, char* argv[]) { MySniffer snf; if(argc == 1 || argc == 3 || argc > 4) { cout << "Wrong options. Read program options.\n" << snf.usage_inf() << endl; exit(1); } for(int i = 1; i < argc; ++i) { if(strcmp(argv[i], "select") == 0) { cout << "hello" << endl; for(int j = i; j < argc; ++j){ if(strcmp(argv[j], "iface") == 0){ string str = string(argv[3]); snf.setIface(str); snf.pkgMonitor(snf.get_cmd()); } } } if(strcmp(argv[i], "show") == 0) { for(int j = i; j < argc; ++j) { if(strcmp(argv[j], " count")){ if(snf.select_data(argv[2])==1){ cout << "There is no adress " << argv[2] << " or data base wasn't created.\n" << "Check if you select network interface.\n"; } snf.pkgMonitor(snf.get_cmd()); } } } if(strcmp(argv[i], "--help") == 0) { for(int i = 0; i < argc; ++i) cout << argv[i] << endl; cout << snf.usage_inf() << endl; snf.pkgMonitor(snf.get_cmd()); } if(strcmp(argv[i], "stop") == 0) { cout << "Packets are not sniffed.\n" << endl; snf.set_cmd(true); } if(strcmp(argv[i], "start") == 0){ if(snf.isIfaceDeff()){ cout << "Packets are being sniffed from now on\n" << endl; } else { cout << "You first need to select your interface. Read program options.\n" << snf.usage_inf() << endl; } snf.set_cmd(false); snf.pkgMonitor(snf.get_cmd()); } } return 0; }