#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void testints(int *x, int *y);
int inputargs();
void printresults();
int inputargs(int argumentnum){
int x;
char a[256];
cout << "Please enter positive integers for" << endl;
cout << "Value "<< argumentnum<<": ";
cin >> a;
x=atoi ( a );
return x; // Return User input
}
void testints(int& x, int& y) {
while ( x<=0 || y<0 || (x<=0 && y<0) || cin.bad()|| cin.peek()!='\n'){
cin.clear();
cin.ignore(100, '\n');
char a[256];
char b[256];
cout << "Invalid input" << endl;
cout << "Please enter positive integers for" << endl;
cout << "Value 1: ";
cin >> a;
x=atoi (a);
cout << "Value 2: ";
cin >> b;
y=atoi (b);
}
}
int GCD(int x, int y) //take in the two variables
{
while( 1 ) //while true
{
if( y == 0 ){ //if y =0 then according to the assignment, the GCD is x
return x;
}
else{
x = x % y;
if( x == 0 )
return y;
y = y % x;
}
}
}
void printresults(int x, int y){
cout << "\nThe Greatest Common Divisor of "<< x << " and " << y << " is " << GCD(x, y) << endl;
system("PAUSE"); //stops the dialog window from closing
}
int main(int argc, char ** argv){
if (argc <=0){ //something weird has happened, terminate
cout << "A Critical Error has occured, system is terminating" << endl;
exit (1);
}
if (argc == 1) //Zero Prompts
{
int argnum1=1;
int x = inputargs(argnum1);
int argnum2=2;
int y = inputargs(argnum2);
testints(x,y);
printresults(x,y);
}
if (argc == 2) // One Command Line
{
int x;
x=atoi(argv[1]);
int argnum2=2;
int y = inputargs(argnum2);
testints(x,y);
printresults(x,y);
}
if (argc == 3) // Two Command Line
{
int x,y;
x=atoi(argv[1]);
y=atoi(argv[2]);
testints(x,y);
printresults(x,y);
}
if (argc > 3) // More then Two Command Line
{
cout << "You have entered more than three inputs" << endl;
cout << "We will only use the first two inputs" << endl;
cout << "Hit enter to acknowledge and continue..." << endl;
int x,y;
x=atoi(argv[1]);
y=atoi(argv[2]);
testints(x,y);
printresults(x,y);
}
else{
return 0;
}
} |