Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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

0
F-J-K
Asked:
F-J-K
1 Solution
 
mrjoltcolaCommented:
Try using:

cin.fail()

instead of cin.failbit

failbit is the actual bit, not the state
0
 
F-J-KAuthor Commented:
Straightforward. Worked...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now