[Solved] if conditions, when i want to second argument [closed]

Introduction

The if condition is a powerful tool in programming that allows you to execute a certain set of instructions based on a certain condition. It is a type of conditional statement that allows you to check if a certain condition is true or false and then execute a certain set of instructions based on the result. In this article, we will discuss how to use the if condition with two arguments and how to solve any issues that may arise.

Solution

If you want to use a second argument in an if condition, you can use an “else if” statement. This statement allows you to specify a second condition that must be met in order for the code to execute. For example:

if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if neither condition1 nor condition2 is true
}

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..