Question

I'm trying to 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:

alt text

How can this be solved?

Thanks.

Was it helpful?

Solution

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 from std individually or qualify them with std::

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top