Solved

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

733 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