void inputGrid(string inputI, unsigned __int64 &i,int t)
{
	do
	{
		t=1;

		getline(cin,inputI);
		i=atol(inputI.c_str());
		if(i<=1)
		{
			cout<<"Please key in only the number that suitable for the grid."<<endl;
			t=0;
			
		}
		
	}
	while(!t);
}

//this is the function i used to solve the problem when i key in char //to an integer...but it fails to function when i set the "if(i<=-1)
//it will still proceed if press enter and not even key in any value

//i want to set the range inside 0 and a size that define by user...

Recommended Answers

All 2 Replies

As i said....i used the function above to handle infinite loop when user key in character to integer input.....but it fail when i change the " i " range to (i<=-1) instead of (i<1). When i press enter without input any value to " i" , the function proceed and take the previous value of " i ". I need a function that can handle the character input that leads to infinite loop and restrict the input between 0 to "size" that define by user= =

anyone can help me please.....T_T

atol returns 0 for empty strings and other non-convertible strings. It doesn't distinguish between the user entering 0 and the user entering jibberish. Since 0 is not less than or equal to -1, an empty string will break out of the loop (your revised loop where the test is -1. Not the one you posted where the test is 1.). Some well placed debugging statements are helpful here (lines 7 and 8 below):

do
{
    t=1;

    getline(cin,inputI);
    i=atol(inputI.c_str());
            cout << "String entered  : " << inputI << endl;
    cout << "atol conversion : " << i      << endl;[/COLOR]
    if(i<=1)
    {
        cout<<"Please key in only the number that suitable for the grid."<<endl;
        t=0;

    }

}
while(!t);

Try it out. 0 will be displayed for empty strings. This is to be expected given the specification.

http://www.cplusplus.com/reference/clibrary/cstdlib/atol/

From that link:

If no valid conversion could be performed, a zero value is returned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.