[英]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? //我的程序假设要从Command Prompt进行操作并拖曳编号,然后对这两个编号应用//操作,但是结果总是错误的,为什么?
#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 . 这是我的代码,如果有更好的方法,请告诉我。
您不能使用==
比较字符串,请使用if(strcmp(argv[1], "+") == 0)
,其余代码也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.