Lara
Lara

Reputation: 43

I have trouble with using command line parameters

// my program suppose to take an operation and tow numbers from Command Prompt and apply // the operation on the two numbers but the result always wrong Why?

#include "stdafx.h"
#include<stdio.h>
#include<tchar.h>
#include<stdlib.h>

int main(int argc, char*argv[])
{
    if(argc !=4)
    {
        printf("number of CLP is incorrect\n");
        return 0;
    }

    int num1 = atoi(argv[2]);
    int num2 = atoi(argv[3]);
    int res ;

    if(argv[1] == "+")
        res = (num1 + num2);

    else if(argv[1]=="-")
        res = (num1-num2);

    else if(argv[1]=="*")
        res = (num1*num2);

    else if(argv[1]=="/")
        res = (num1/num2);

    printf("You enterd Operation %s and the Resualt = %d\n" , argv[1] , res);
    return 0;
}

this is my code , and if there is a better way to do it let me know .

Upvotes: 0

Views: 65

Answers (1)

P.P
P.P

Reputation: 121427

You can't compare strings with == Use if(strcmp(argv[1], "+") == 0) and likewise in the rest of the code.

Upvotes: 3

Related Questions