Modulo Operator and If-Clause
Hi,
I'm a Beginner in C++ and I have a problem with the Modulo operator.
I'm writing a testprogram with following sourcecode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
using namespace std;
int main ()
{
//Definition Variablen
int d, e, erg;
cout<<"Zahl 1 eingeben: \n";
cin>>e;
cout<<"Zahl 2 eingeben: \n";
cin>>d;
if (erg = e % d == 0)
{
cout<<"Der Rest ist 0 \n";
}
else
{
cout<<"Der Rest ist: "<<erg<<endl;
}
system ("Pause");
return 0;
}
|
After starting the program I have to type in the two numbers.
Then it should compare if the rest = 0.
Then it should say "Rest is 0" or "Rest is erg"
But whatever I type in the both variables, everytime the output is "Rest is 0".
Can anybody tell me what is my mistake?
Best regards
if (erg = e % d == 0)
doesn't do what you want. You can fix your code in this way:
1 2 3
|
...
if ((erg = e % d )== 0)
...
|
or
1 2 3 4
|
...
erg=e%d;
if (erg == 0)
...
|
Last edited on
Thanks it's working ;-)
Topic archived. No new replies allowed.