I've been trying to write a program that factors number for me but my if statement keeps running despite the condition being false. In function factor, I use modf to separate the decimal from the integer and store it as c. The if statement checks if c = 0 which means the number divided evenly.
Below is my source code with results below it.
#include <iostream>
#include <math.h>
using namespace std;
int factor(double b);
int i = 1;
int factors[] = {1};
int main()
{
int a;
double b;
cout << "Please enter a integer to Factor: ";
cin >> a;
b = a;
factor(b);
return 0;
}
int factor(double b)
{
double c;
double d;
for ( ; c != 0 ; i++)
{
cout << "for loop executed" << endl;
c = modf (b/3, &d);
cout << c << endl;
if (c = 0.00);
{
cout << 3 << endl;
factors[i] = 3;
continue;
}
c = modf (b/5, &d);
if (c = 0);
{
cout << 5 << endl;
factors[i] = 5;
continue;
}
c = modf (b/7, &d);
if (c = 0);
{
cout << 7 << endl;
factors[i] = 7;
continue;
}
c = modf (b/11, &d);
if (c = 0);
{
cout << 11 << endl;
factors[i] = 11;
continue;
}
c = modf (b/13, &d);
if (c = 0);
{
cout << 13 << endl;
factors[i] = 13;
continue;
}
c = modf (b/17, &d);
if (c = 0);
{
cout << 17 << endl;
factors[i] = 17;
continue;
}
}
return c;
}
In cmd it prints
Please enter a integer to Factor: 50
for loop executed
0.666667
3
50/3=16.6 repeating
modf of 16.6666666 outputs 16 and 0.666667
0.666667 does not equal 0 so im confused.
Aucun commentaire:
Enregistrer un commentaire