-8
  #include<fstream>
  #include <iostream>
  #include<string.h>
  using namespace std;

 int main(int argc, char* argv[])
 {
         string s = "all";
         string t = "top";

    for (int i=1; i<argc ; i++)
           {

                  if( i == 2 && (argv[i] != s) && (argv[i] != t))
                       {

                            cout<<"INVALID MODE"<< endl;
                        }
                  if( i ==1 && (argv[i] != int))

                       {  

                            cout<<"INVALID PHRASE LENGTH"<< endl;

                        }



           }



 }

I know this is horribly wrong, but is there away to do this ? im trying to say that when i =2, meaning argument 2 in a command line, is not all, and not top, to print out InValid mode

along with that, how would i test if an argument is not an integer and if it is not an integer to print out “INVALID PHRASE LENGTH” , but how would i test if the first argument is an integer or negative number

  • 5
    `1 `i == 2`. – NathanOliver Sep 20 '18 at 13:24
  • 13
    I'll re-iterate [a comment on your previous question](https://stackoverflow.com/questions/52424432/command-line-argument-cant-or-two-variables#comment91791625_52424432): stop trying syntax until something compiles. Read a good beginner book on C++. – StoryTeller - Unslander Monica Sep 20 '18 at 13:24
  • if( i == 2 && (argv[i] != s) && (argv[i] != t)) – Andrey Chernukha Sep 20 '18 at 13:26
  • 1
    There are too many questions for one question. Try to solve one problem at a time, instead of trying to parse all arguments at once. – Yksisarvinen Sep 20 '18 at 13:29
  • Instead of all these stories just give how do you run your program with all the arguments that might help. – kiran Biradar Sep 20 '18 at 13:30
  • For integer part see https://stackoverflow.com/questions/30348598/how-to-check-if-argvcount-is-an-integer – kelalaka Sep 20 '18 at 13:37
  • 2
    @scohe001 The horrifying fact: Without _the pesky `int`_ it compiles and is always true: [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/6a0d2aef134ae373) as any comparison yields false or true, converted to 0 or 1 which is always `< 3`. ;-) – Scheff's Cat Sep 20 '18 at 13:40
  • @Scheff derp. Clearly it's too early in the morning for me when I start reading classic SO C++ mistakes as working code. Thanks for that call out :) – scohe001 Sep 20 '18 at 14:12
  • 1
    Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UKMonkey Sep 20 '18 at 14:43

1 Answers1

-1

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

  • It would be great if you could provide a comment for the negative rating. It could help me and other improve on the answer. – Raven221221221 Sep 21 '18 at 10:23