Not sure if you are aware, but when you run (sorry for running on windows)
program.exe arg1 arg2
then argv[0]
is program.exe
, argv[1]
is arg1
, argv[2]
is arg2
, so careful what you call the 1st and 2nd argument, meaning argv[1]
is indeed the first string after the binary name, but only because of C++ indexing that starts from 0
.
From what you are trying to achieve there is no need for the loop and iterating over the arguments.
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
string s = "all";
string t = "top";
if (argc >= 3 && ! (argv[2] == s || argv[2] == t)) {
cout << "INVALID MODE" << endl;
}
}
There are plenty of questions answering parsing a string into int
.
As said here though, picking up C++ beginner book is a better time investment than trying what compiles..
1
solved if conditions, when i want to second argument [closed]