我對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;
}
哪行做了錯誤發生在? – user2079303
@ user2079303因爲我一直在讀,所以可能是在main()中:'char z;/* ... */cin >> z;'** edit **:或_very near there_ - 當您看到它時... –
請以理智的方式格式化您的代碼。另外,仔細看看你的編譯器錯誤的行。該行不完全正確。 – Barry