2
#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++)
    {
        cout<< argv[i]<< endl;
        if( argv[2] != s || t)
        {
            cout<<"INVALID MODE"<< endl;
            return -1;
        }
    }
}

No viable conversion from 'std::__1::string' (aka 'basic_string, allocator >') to 'bool'Invalid operands to binary

expression ('bool' and 'std::__1::string' (aka 'basic_string, allocator >'))

hello, i am passing command line arguments such as "1 all emptyfile" and "1 top emptyfile", here i want to test if the second argument is not all or top to print out "INVALID MODE". i am having trouble understanding why i cant use the s||t. i get the errors i posted above, is there anyway around this

ive gotten help, thanks to this site, but i have another question, if i have top or all as the second argument, i want it NOT to print out " INVALID MODE". but it seems to be still printout "invalid mode" where in this case all is the second argument, but doesnt seem to print when top is the second argument.

AGAIN thanks so much, another thing i have trouble with, is how to print out "NO PHRASE LENGTH" when no arguments are passed? how does one test that.

also how do i test if the first argument is an integer?, so it if its not an integer and is negative number print out "Invalid phrase length"

2 Answers2

4

Your expression argv[2] != s || t is grouped as (argv[2] != s) || t, the left hand argument of || is then a bool type and that's a problem since there is no overloaded || operator for std::string that takes a bool. That explains the cryptic compiler message.

The solution here is to write

argv[2] != s && argv[2] != t

instead. Note that I've switched || for && else the conditional check is always true and your program would always output "INVALID MODE"! Some languages work the way you have it; C++ doesn't.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You have to test like this

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

Some scripting languages allow you to do comparisons like you have done in the question. PERL is one such language.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • ...where I've added excess parentheses for clarity / I'm a quiche eater / I'm a girl / I can't be bothered to commit the operator precedence table to memory. – Bathsheba Sep 20 '18 at 11:50
  • Or dietary habits either? It's a nice answer though. +1 for good feelings. – Bathsheba Sep 20 '18 at 11:53
  • 1
    Thanks. Upped your answer too. – P.W Sep 20 '18 at 11:54
  • thank you, that seems to clear the error, but i have another question, if i have top or all as the second argument, i want it NOT to print out " INVALID MODE". but it seems to be still printout "invalid mode" where in this case all is the second argument. – user10388866 Sep 20 '18 at 11:56
  • 1
    @user10388866: You need `&&` rather than `||` – Bathsheba Sep 20 '18 at 12:00
  • Yes, I chose PERL over Python years back: an action similar to my father when he purchased a Betamax video player. – Bathsheba Sep 20 '18 at 12:09
  • 1
    @P.W. My favourite Birman cat at home (and I have four) is called Bathsheba. Named after Bathsheba Everdene in "Far from the Madding Crowd", or King David's better half. – Bathsheba Sep 20 '18 at 12:11
  • 1
    Yeah, David's 8th wife. Can't fault him though. He was a king after all. – P.W Sep 20 '18 at 12:18