开发者

C++ modulus small program issue

开发者 https://www.devze.com 2023-02-05 06:05 出处:网络
I\'m trying t开发者_运维知识库o write a very simple program in C++ that finds the modulus of two numbers as follows:

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:

C++ modulus small program issue

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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消