C++模数小程序问题

Sim*_*ity 1 c++ modulus

我正在尝试用C++编写一个非常简单的程序,它找到两个数字的模数,如下所示:

#include <iostream>
using namespace std;
int n;
int d;
int modulus;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
cin>>d;
modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试编译它时,我得到以下内容:

替代文字

怎么解决这个问题?

谢谢.

sep*_*p2k 9

您收到错误,因为全局变量的名称modulus与之冲突std::modulus.要解决此问题,您可以:

  • 创建modulus一个局部变量
  • 重命名modulus变量
  • 删除using namespace std并从std单独导入您需要的名称或使用它们进行限定std::