I would like to have the user enter ctrl-z to end the program...can someone tell me how to insert this into the following program?

//Program to calculate greatest common denominator of two integers

#include <iostream>

using namespace std; 

int calcGCD(int, int);

int main()
{
	int a, b, res;
	
			
		cout <<"This program will determine the greatest common divisor of "
			<< "two positive integers. ";
		cout <<endl;
		cout << "Please input your first positive number: ";
		cin >> a;
		cout << "Please input your second positive number: ";
		cin >> b;
		cout <<endl;

		while ( a > 0 && b > 0 )
		{
			res = calcGCD ( a, b );
     
			cout << "Greatest common divisior (GCD) of "<< a <<" and "<<b<<" is "<<res;
			cout <<endl;
			cout <<endl;
			cout << "Please input your first positive number: (or ctrl-z to end program) ";
			cin >> a;
			cout << "Please input your second positive number: ";
			cin >> b;
		}
		
		system ("pause");
		return 0;
}
int calcGCD (int a, int b)
{
	int c;

	if ( a < b )
		swap (a, b);

	c = a % b;
	if ( c == 0 )
		return b;
	calcGCD ( b, c );
}
/*

				if ( b > a )
					swap (a, b);
				while ( b != 0 )
				{
					temp = b;
					b = a - b;
					a = temp;
					if ( b > a )
						swap (a, b);
				}
				return GCD;

 
	 system ("pause");
	 return 0;

 }*/

When cin encounters Ctrl-Z, cin.eof() will return true. So e.g.

if(cin.eof())
{
  cout << "bye ...";
  return 0;
}

Why not just hit Ctrl-C. It'll stop the program too. :icon_wink:

You need to test the return from cin and look for EOF

You need to test the return from cin and look for EOF

Am I missing something (big time), or isn't that just what

if(cin.eof())

does?

Yes it does. But when I opened the thread at around 3:12 the other posts weren't there. Then I got called away and finished my post at 3:37.

Edited 6 Years Ago by WaltP: n/a

Yes it does. But when I opened the thread at around 3:12 the other posts weren't there. Then I got called away and finished my post at 3:37.

LOL, a nice mix-up.

This question has already been answered. Start a new discussion instead.