C ++ Modul kleines Programm Ausgabe
Frage
Ich versuche, ein sehr einfaches Programm in C ++ zu schreiben, die den Modul von zwei Zahlen findet wie folgt:
#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;
}
Aber, wenn ich versuche, es zu kompilieren, ich folgende erhalten:
Wie kann dieses Problem gelöst werden?
Danke.
Lösung
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::
Andere Tipps
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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow