首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于含参的main函数中的一个有关问题

2012-11-07 
关于含参的main函数中的一个问题。#includeiostreamusing namespace stdint main(int argc,char **argv)

关于含参的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。

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;} 

热点排行