473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Clearing buffer

dev7060
636 Expert 512MB
[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, 775 views)
File Type: jpg 2.jpg (127.4 KB, 1872 views)
Sep 18 '18 #1
1 2414
weaknessforcats
9,208 Expert Mod 8TB
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
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 =...
5
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...
6
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...
4
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
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
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
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...
12
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...
9
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...
2
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.