Solved

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

Posted on 2009-03-29
2
322 Views
Last Modified: 2012-05-06
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
Comment
Question by:F-J-K
2 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24016066
Try using:

cin.fail()

instead of cin.failbit

failbit is the actual bit, not the state
0
 
LVL 1

Author Closing Comment

by:F-J-K
ID: 31564191
Straightforward. Worked...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question