So basically I am trying to develop a simple application that uses a function to determine whether from 2 numbers input by a user, the second number is a multiple of the first.
#include<iostream>
using namespace std;
int multiple( int n1, int n2){
if(n1%n2 ==0)return 1;
else return 0;
}
int main(){
int num1, num2;
cout<<"Enter 2 numbers (-1 to exit): ";
cin>>num1>>num2;
while (num1 !=-1 && num2 != -1){
if (multiple (num1, num2)==1)cout<<num2<<" is a multiple of "<<num1<<endl<<endl;
else cout<<num2<<" is not a multiple of "<<num1<<endl<<endl;
cout<<"Enter 2 numbers (-1 to exit): ";
cin>>num1>>num2;
}
return 0;
}
Now when I try it with normal integers it works fine,but why is it that when I input the second number as a decimal, it enters an infinite loop? Specifically this is the statement it keeps giving:
"Enter 2 numbers (-1 to exit): 1 is a multiple of 0"
The debugger shows that num1
becomes 0
for some weird reason. I know I can overcome this by using an if statement, but for my own curiosity can anybody out there explain why this happens? I can provide you with any other thing you require, and I am using xCode
if that is of any relevance.