Thread: system("PAUSE");

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Lightbulb 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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed