Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

How Can I Check Overflows? - I used cin.getline() - C++

Both examples doesn't work. User is expected to insert chars less than MAX_DATA length, but i want to handle the situation when user inserts over MAX_DATA. I tried several examples, non of them worked...
char userInput[MAX_DATA];
               cin.getline(userInput, MAX_DATA);
		if(cin.failbit)
		{
			cout<<"Overflow occured. Restart Application"<<endl;
			system("pause");
			exit(-1);
		}
 
//***************************************************************//
 
               cin.getline(userInput, MAX_DATA);
		if(strlen(userInput) > MAX_DATA)
		{
			cout<<"Overflow occured. Restart Application"<<endl;
			system("pause");
			exit(-1);
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

Straightforward. Worked...