2016-04-12 49 views
-5

我对C++有点新鲜,我试图编写一个代码来制作一个基本的C++计算器,但是当我尝试构建它时弹出如下错误:C++错误:'操作符>>'不匹配'

“错误:'operator >>'不匹配(操作数类型是'std :: basic_ostream :: __ ostream_type {aka std :: basic_ostream}'和'char')|”

的代码是:

#include <iostream> 
#include <string.h> 

using namespace std; 

void add(int x, int y) 
{ cout<<"First number:"; 
cin>>x; 
cout<<endl<<"Second number:"; 
cin>>y; 
cout<<endl<<x<<"+"<<y<<"="<<x+y; 
} 

void subtract(int x, int y) 
{ 
cout<<"First number:"; 
cin>>x; 
cout<<endl<<"Second number:"; 
cin>>y; 
cout<<endl<<x<<"-"<<y<<"="<<x-y; 
} 

void multiply(int x, int y) 
{ 
cout<<"First number:"; 
cin>>x; 
cout<<endl<<"Second number:"; 
cin>>y; 
cout<<endl<<x<<"*"<<y<<"="<<x*y; 
} 

void divide(int x, int y) 
{ 
cout<<"First number:"; 
cin>>x; 
cout<<endl<<"Second number:"; 
cin>>y; 
cout<<endl<<x<<"/"<<y<<"="<<x/y; 
} 

int main() 
{ 
int x,y; 
char z; 
cout << "\t\t\t Welcome to the C++ Calculator." << endl; 
cout << "What operation would you like to perform? "; 
cout << "Add(+), Subtract(-), Multiply(*), Divide(/):"<< 
cin >> z; 
cout <<endl; 
if (z=='+') 
    add(x,y); 
else if (z=='-') 
    subtract(x,y); 
else if (z=='*') 
    multiply(x,y); 
else if (z=='/') 
    divide(x,y); 
else if ((z!='+')&&(z!='-')&&(z!='*')&&(z!='/')) 
    cout<<"Unknown symbol."; 
return 0; 
} 
+4

哪行做了错误发生在? – user2079303

+1

@ user2079303因为我一直在读,所以可能是在main()中:'char z;/* ... */cin >> z;'** edit **:或_very near there_ - 当您看到它时... –

+1

请以理智的方式格式化您的代码。另外,仔细看看你的编译器错误的行。该行不完全正确。 – Barry

回答

0

尝试

COUT < < “添加(+),减( - ),乘(*),除(/):” ;

代替

COUT < < “添加(+),减( - ),乘(*),除(/):” < <

+0

就是这样,那就是问题所在。我刚刚意识到错误是多么的微小,只是因为我缺乏关注。感谢您帮助并抱歉忍受我的“不敬”。 –

+0

如果在函数内完成输入,则不应将x和y作为参数。使用一个空的参数列表并将x和y声明为局部变量。 –

相关问题