//Calculates GCF and LCD from user input repeatedly
# include <iostream.h> //needed for text
usingnamespace std;
int getGCF(int x, int y)
{
int z;
while (y != 0)
{
z = x % y;
x = y;
y = z;
}
return x;
}
int main ()
{
char repeat = 'y';
while (repeat == 'y')
{
int A, B, D, repeat;
int thousandfctrA, thousandfctrB;
cout << "This program calculates the GCF and LCM of two integers. ";
cout << endl;
cout << endl;
cout << "Please enter the first positive integer. ";
cout << endl;
cin >> A;
cout << endl;
thousandfctrA=A*1000;
while (A<=0 || (thousandfctrA%1000)!=0)
{
cout << "That is not a positive integer. ";
cout << endl;
cout << "Please enter the first positive integer. ";
cout << endl;
cin >> A;
cout << endl;
}
//if input is correct
cout << "Please enter the second integer. ";
cout << endl;
cin >> B;
cout << endl;
thousandfctrB=B*1000;
while (B<=0 || (thousandfctrB%1000)!=0)
{
cout << "That is not a positive integer. ";
cout << endl;
cout << "Please enter the second positive integer. ";
cout << endl;
cin >> B;
cout << endl;
}
//for correct input
cout << "The GCF of " <<A<< " and " <<B<< " is " <<getGCF(A,B)<< endl;
cout << endl;
D=(A*B)/getGCF(A, B); //LCM equation
cout << "The LCM of " <<A<< " and " <<B<< " is " <<D<< endl;
cout << endl;
cin.clear();
cout << "Do you wish to calculate again? ";
cout << endl;
cout << "If so, press 'y'. If not, enter 'n'. ";
cin >> repeat;
cout << endl;
cout << endl;
}
int enter;
cout << "Bye!";
cin.clear();
cin >> enter;
return 0;
}
Thanks anyway, guys. I needed to change it to a do while loop, and change line 18 to char repeat; instead.
Thanks for the info about the iostream though!