473,668 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing buffer

dev7060
644 Recognized Expert Contributor
[Screenshot 1] The screen is not stopping on cin.get() functions for the user to input something. Maybe automatically taking the "enter key" from the buffer.
[Screenshot 2] On using some functions to clear the buffer (like cin.ignore() and fflush(stdin) ), everything is working as required.

My questions are:
(If I am getting something wrong, please point out)

-Why does cin.get() automatically taking the "enter key" from the buffer (previous inputs)?
- Why do simple implementations of cins not follow this thing?
Like:
Expand|Select|Wrap|Line Numbers
  1. cout<<"enter something"; cin>>a;
  2. cout<<"enter something"; cin>>b;
  3. cout<<"enter something"; cin>>c;
  4.  // Here the screen stops everytime
-Are these functions (of clearing buffers) especially designed for such cases? Could you provide an example scenario where we don't need those functions? I mean why would someone want to access the previous buffer data of "enter key".
Attached Images
File Type: jpg 1.jpg (100.1 KB, 793 views)
File Type: jpg 2.jpg (127.4 KB, 1914 views)
Sep 18 '18 #1
1 2456
weaknessforcats
9,208 Recognized Expert Moderator Expert
I don't see any error checking here. How do you know the cin>> worked?

If it gets an error, the data is left in the buffer and the cin>> quits. The next cin>> sees the error and bails without even trying.


This makes it appear all the cin>> quit working for the rest of the program.

Maybe something like this:

Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int* data)
  2. {
  3.     cin >> *data;
  4.     if (cin.good())
  5.     {
  6.       return true;
  7.     }
  8.     else
  9.     {
  10.         cin.clear();
  11.         _flushall();  //Microsoft only
  12.         return false;
  13.     }
  14. }
Sep 18 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
7491
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer = "value1" + " : " + "value2";
5
817
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears flags (bits) in the stream, right? I solved it by declaring the stringstream inside the loop body even though I have need of another stringstream just after the loop. So for each iteration the constructor of the stringstream will be executed. Is...
6
31064
by: Carol Pedder | last post by:
Hello all, may I ask a question? Having just started C++ (using microsoft visual studio), I am using cin/cout for console applicarions (yes I know it doesn't happen in the real world!) and I'm finding it difficult to track down a method of clearing the input buffer after a cin (to get rid of potential rubbish). cin.flush() has been mentioned, but isn't an option on visual studio and cin.ignore() requires you to specify number of chars...
4
7463
by: news.eircom.net | last post by:
Hello, How do you clear the contents of an ostringstream object? I've included some example code so you can see what I mean. t.i.a. Craig H. #include <iostream>
28
16608
by: Terry Andersen | last post by:
I have an array that I initialize to zero, like: Buffer = {0x00}; How do I in my code reset this array to all zeros ones more? Without running a whole for loop? Best Regards Terry
4
9533
by: Roubles | last post by:
Hi All, Quick question; what is the difference between initializing and clearing a structure like this: mystruct_t a = {0}; and initializing and clearing it like this:
1
1946
by: kelharis | last post by:
Hello, I am writing a program that will run once, and once done, ask the user if they would like to use it again. It has to work with any answer beginning with "y". I am having trouble with clearing the buffer when I answer "yes" or anything other than "y" itself. It works perfectly when "y" is entered, now I just need to somehow accept the other yes words and clear the buffer once the "y" has been consumed. code is as follows: ...
12
4282
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls a data set which can have a lot of information and it is concatenated in this single string variable object. When the daemon finishes its job, it goes to sleep, but before doing so, it "clears" this variable so it can be reused again in the next...
9
2798
by: kardon33 | last post by:
Hi all, What i am trying to accomplish is take in a list of IP addresses from a text file (each Ip is on a new line). I got my program mostly working except when an IP address is longer than the last one it keeps the extra numbers beacuse im having trouble clearing the IP.
2
8106
by: KiddoGuy | last post by:
I have the following code int parseFile(FILE* fp, char s) { char *buffer; int c, i; i = 0; buffer = (char*)malloc(MAX_ARRAY+1); while ((c = getc(fp)) != EOF) { while (!isspace(c)) {
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8374
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.