Solved

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

Posted on 2009-03-29
2
319 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
Comment Utility
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
Comment Utility
Straightforward. Worked...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 writing to files in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

744 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

18 Experts available now in Live!

Get 1:1 Help Now