关于含参的main函数中的一个有关问题
关于含参的main函数中的一个问题。
#include<iostream>
using namespace std;
int main(int argc,char **argv)
{
if(argc!=3){
cout<<"you should use three arguments!"<<endl;
system("PAUSE");
return -1;
}
cout<<"Summation of "<<argv[1]
<<"and"<<argv[2]<<"is"
<<(atof(argv[1])+atof(argv[2]))<<endl;
system("PAUSE");
return 0;
}
源程序是这样 通过编译器编译生成 main.exe文件
再通过命令行调用。main.exe aa bb
最后的两个参数无论输什么,输出的结果都是0
------解决方案--------------------
如下这样修改,然后你用了atof,所以输入浮点数才有有正确的结果,例如main.exe 12 24。
#include<iostream>
using namespace std;
int main(int argc,char **argv)
{
if(argc!=3){
cout<<"you should use three arguments!"<<endl;
system("PAUSE");
return -1;
}
cout<<"Summation of "<<argv[1]
<<"and"<<argv[2]<<"is"
<<(atof(argv[1])+atof(argv[2]))<<endl;
system("PAUSE");
return 0;
}
源程序是这样 通过编译器编译生成 main.exe文件
再通过命令行调用。main.exe aa bb
最后的两个参数无论输什么,输出的结果都是0
------解决方案--------------------
如下这样修改,然后你用了atof,所以输入浮点数才有有正确的结果,例如main.exe 12 24。
- C/C++ code
#include <stdlib.h> #include<iostream> using namespace std; int main(int argc,char **argv) { if(argc!=3){ cout<<"you should use three arguments!"<<endl; system("PAUSE"); return -1; } cout<<"Summation of "<<argv[1] <<" and "<<argv[2]<<" is " <<(atof(argv[1])+atof(argv[2]))<<endl; system("PAUSE"); return 0; }