C++ Why does it pause?

I have to write a C++ program to get the GCD based on inputs from user. I completed it except for one detail. If I use command arguments, something my program detects, I for some reason have to hit enter before the program does anything.
My code is available below

Can someone please help me. I can't figure out what is with this pause business. The rest of the program executes perfectly except for this one thing.

Edit: Just figured out this issue only happens when 2 or more command arguments are passed. Works fine with one.

#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;
	}
}
Last edited on
For valid input - say x==35 and y==7 - cin.peek()!='\n' will wait till a new line is entered.

1
2
3
4
5
6
7
8
9
10
11
void testints(int& x, int& y) 
{
	while ( 
                x<=0 /* false */ || 
                y<0 /* false */  || 
                (x<=0 && y<0) /* false */ || 
                cin.bad() /* false */ || 
                cin.peek()!='\n' // since everything before this is false, check this now
              )
        {
               // ... 
@JLBorges
There's two issues
First off, my testints is not stopping a string to be entered for y.
Also, the enter issue is still here.

The reason for #1 I know is that atoi is failing for y on the string and thus returns 0, but because a 0 is a legit number for y in the GCD formula, it keeps it. A 0 for x is not and thus it is stopped during the test ints. Is there a way I can check the string before atoi to make sure its a number somehow?
Last edited on
Solved issue 2 by removing removed || cin.bad() || cin.peek()!='\n' completely. Just need to figure out what to do with strings for y.
Still need help with stopping strings for int y. Any ideas?
Figured it out:
int getValidIntFor2(int min)
{
	int number;
	while ( (cout << "Enter a valid number for value 2: " && !(cin >> number) )  || (number < min) || cin.peek() != '\n')
	{
		cout << "You must input an integer value" <<  endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	return number;
}
Last edited on
> Is there a way I can check the string before atoi to make sure its a number somehow?

There certainly is, but ...

As it is, your code is way too complex for me to handle with any degree of comfort. Adding this check would make it even more complicated.

Try and make it simpler, perhaps? For instance, test the numbers for validity one by one instead of all at once?

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int get_value( int arg_num, int min_value )
{
    int value ;
    std::cout << "value " << arg_num << "? " ;
    if( std::cin >> value && value >= min_value ) return value ;
    else
    {
        std::cerr << "error in input... " ;
        std::cin.clear() ; // 
        std::cin.ignore( 1024, '\n' ) ;
        return get_value( arg_num, min_value ) ;
    }
}

int main( int argc, char* argv[] )
{
    enum { MINX = 1, MINY = 0 } ;
    int x = -1 ;
    int y = -1 ;
    
    if( argc > 1 ) x = std::atoi(argv[1]) ;
    if( argc > 2 ) y = std::atoi(argv[2]) ;

    if( x < MINX ) x = get_value( 1, MINX ) ;
    if( y < MINY ) y = get_value( 2, MINY ) ;

    // compute gcd(x,y) and print it out
}
Last edited on
Topic archived. No new replies allowed.