-2

I'm trying to pass arguments through main, which works fine, I then check to see if the passed in argument contains the correct format/value. However, even if I pass through the correct format it still shows that there is something wrong, here is the code:

int main(int argc, char* argv[]) {

/* Check if arguments are being passed through */ 

if(argc == 1){
    cout << endl << "--- ERROR ---" << endl;
    exit(0);
}

/* Check if the first argument contains the correct data */
string file_name = argv[1];

/* Handle operation */

string operation = argv[2];

if(operation != "-t" || operation != "-r")
{
    cout << "Something is not right";
}

}

If I do: cout << operation; then the result would be: -t when passing -t through when I run the applications.

Could anyone suggest where I could be going wrong?

UPDATE:

I will pass in these arguments:

./main something.wav -t

I am expecting the if statement:

if(operation != "-t" || operation != "-r")
{
    cout << "Something is not right";
}

To return negative since the value I have entered is -t

4

1 に答える 1

5
if(operation != "-t" || operation != "-r")
{
    cout << "Something is not right";
}

操作が何であれ、「-t」または「-r」と等しくない必要があるため、常に「何かが正しくありません」と出力されます。

私はifステートメントを期待しています:
私が入力した値が-tなので負を返す

OR の後半は真です。前半または後半のいずれかが真である場合、OR は真です。あなたがしたい((operation != "-t") && (operation != "-r"))。そうifすれば、入力が -t ではなく、-r でもない場合にのみ起動します。

于 2013-03-18T13:24:59.167 に答える