Solved

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

Posted on 2009-03-29
2
320 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 107
Visual Studio 2010 There is no editor available for ... 8 79
Problem to save 10 151
Microsoft C++ code failing in executable that worked 9 86
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now