-
system("PAUSE");
Code:
// euclid.cpp - Compute Greatest Common Divisor of two positive integers
// using the Euclidean Algorithm
#include <iostream>
#include<cstdlib>
// Get a positive integer from user
int getPositive(void){
int current;
while( 1 ){
cout << "Please enter a positive integer: ";
cin >> current;
if (current > 0) return current;
cout << "It should be a positive integer";
}
}
// Compute GCD
int gcd (int dividend, int divisor)
{
int remaind;
remaind = dividend % divisor;
while (remaind != 0) {
dividend= divisor;
divisor= remaind;
remaind= dividend % divisor;
}
return divisor;
}
void main(void)
{
int dividend = getPositive();
int divisor = getPositive();
cout << "The GCD of " << dividend << " and " << divisor << " is "
<< gcd(dividend, divisor) << endl;
system("PAUSE");
return 0;
}
I cant see the output. y? is that? Although i have used system("PAUSE"); thing.
-
Post your compile log.
Also, read up on this article and on namespaces
That said, a possible corrected version of your program is:
Code:
// euclid.cpp - Compute Greatest Common Divisor of two positive integers
// using the Euclidean Algorithm
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
// Get a positive integer from user
int getPositive() {
for (;;) {
int current;
cout << "Please enter a positive integer: ";
cin >> current;
if (current > 0)
return current;
cout << "It should be a positive integer\n";
}
}
// Compute GCD
int gcd(int dividend, int divisor)
{
int remaind = dividend % divisor;
while (remaind != 0) {
dividend = divisor;
divisor = remaind;
remaind = dividend % divisor;
}
return divisor;
}
int main()
{
int dividend = getPositive();
int divisor = getPositive();
cout << "The GCD of " << dividend << " and " << divisor << " is "
<< gcd(dividend, divisor) << endl;
system("PAUSE");
return 0;
}
-
Don't use a non-standard operating system command to do something that you can do in the language itself.
Use cin or some form thereof.