I'm trying t开发者_运维知识库o write a very simple program in C++ that finds the modulus of two numbers as follows:
#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;
}
But, when I try to compile it, I get the following:
How can this be solved?
Thanks.
You get the error because the name of your global variable modulus
clashes with std::modulus
. To fix this, you can:
- Make
modulus
a local variable - Rename the
modulus
variable - Remove
using namespace std
and either import the names you need fromstd
individually or qualify them withstd::
Because you have using namespace std;
it clashes with std::modulus
Corrected version:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
int n;
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
int d;
cin>>d;
int modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
精彩评论