Solved

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

Posted on 2009-03-29
2
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

717 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