希望修复 main.cpp 与"std::istream&<< int&"运算符不匹配



首先,下面是代码:

#include <iostream>
#include <stdlib.h>
using namespace std;
int add(int x,int y){
    return x+y;
}
int sub(int x,int y){
    return x-y;
}
int divide(int x,int y){
    return x/y;
}
int mult(int x,int y){
    return x*y;
}
void operation(string operation,int x,int y){
    if(operation=="addition"){
        cout << add(x,y);
    }
    else if(operation=="subtraction"){
        cout << sub(x,y);
    }
    else if(operation=="multiplication"){
        cout << mult(x,y);
    }
    else if(operation=="division"){
        cout << divide(x,y);
    }
}
int main(int argc, char *argv[]){
  while(true){
    string operation;
    int x;
    int y;
    cout << "Please enter your operation: ";
    cin >> operation;
    cout << endl << "Please enter the first number: ";
    cin << x;
    cout << endl << "Please enter the second number: ";
    cin << y;
    cout << endl;
    operation(operation,x,y)
    }
  }
  system("PAUSE");  
  return 0;
}

我遇到的问题是我想让用户使用 cin 输入 x 和 y,但由于某种原因,我得到了标题中显示的错误(主要.cpp与"std::istream&<<int&"运算符不匹配)我认为这可能与我初始化变量的方式有关,但我不知道它是否可能是其他东西。

感谢您的帮助!

看看 A Gentle Introduction to C++ IO Streams。您应该将<<用于cout,>>用于cin。

最新更新