注册 登录
编程论坛 C++教室

请教一下,为什么结果不正确

okay011 发布于 2012-07-12 16:23, 363 次点击
为什么以下程序得不到我想要的结果? 生成程序文件test.exe cmd命令行输入test 2+2 ,应该有结果显示,但什么也没显示。。。。错哪了?路径都正确

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main(int argc,char** argv){
    {if(argc!=2)
    cout<<"usage: test command\n";
    return 1;}
  string s(argv[1]);
  istringstream  sin(s);
  int a,b;  char c;
  sin>>a>>c>>b;
  switch(c){
     case '-':cout<<a-b;break;
     case '+':cout<<a+b;break;
     case '*':cout<<a*b;break;
     case '/':cout<<a/b;break;
     default:cout<<"错误";
    }
  }
2 回复
#2
rjsp2012-07-12 16:39
你不是 return 1; 了吗?
代码不排版就是耍流氓,害人更害己
#3
okay0112012-07-12 16:59
我重新排版,而且修改了一下:
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main(int argc,char** argv){
  if(argc!=2)
    cout<<"usage: test command\n";
  else{
     string s(argv[1]);
     istringstream  sin(s);
     int a,b;  char c;
     sin>>a>>c>>b;     
    switch(c){
     case '-':cout<<a-b;break;
     case '+':cout<<a+b;break;
     case '*':cout<<a*b;break;
     case '/':cout<<a/b;break;
     default:cout<<"错误";
                         }
      }

}  
-----------------------------
 if(argc!=2)                          // 这行什么意思?argc不等于2?有什么功能?我接教~~
    cout<<"usage: test command\n";   
1