Would this work?

for getting the size of the cin buffer?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int getsize(istream & file)
{
    int length, pos = file.tellg();
    file.seekg(0, ios::end);
    length = file.tellg();
    file.seekg(pos, ios::beg);
    return length;
}

bool empty(istream & file)
{
    if ( file.ignore(getsize(file), '\n') )
        return true;
    else return false;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    bool ok;
    int x;
    //char x;
    be4:
    cout << "Enter something.\n";
    cin >> x;
    if (cin.fail())
        {
            cin.clear();
            ok = empty(cin);
            cin.clear();
        }
    goto be4;
    main();
    return int( (char)main()) + ok;
}


getsize(cin) returns -1, but cin.ignore(-1, '\n'); still seems to work. What is happening here?
No, the way you are handeling cin isn't supported.
Also:

1) Don't use goto the way you are, it's not needed
2) Calling main is non-standard, don't do it
3) return int( (char)main()) + ok;
Wtf is this?? Even if you could call main it makes no sense
All very good points firedraco, I didn't even read into it that far.

@ OP: What language are you coming off of? You seem to have some of your wires crossed and they NEED to be fixed soon if you plan on writing in C\C++
I meant that last bit as a joke, but it still compiles under codeblocks using mingw32.

and also my code that I posted does in fact seem to work, but I don't understand why getsize(cin) is returning
-1
and I don't understand what cin.ignore(-1, '\n') is actually doing. Could it be that the prototype for cin.ignore() has some unsigned datatype that interprets -1 as a very large number? Because my call to empty(cin) seems to be clearing the buffer of all of its contents, i.e. its working as I would expect it to work as if getsize() was returning the size of the cin buffer (which it isn't).

So what is it doing?

And to be frank, I think it should be obvious what it is I'm trying to do, so would someone please kindly post correct code? i.e. returns the size of the cin.buffer and empties it?

Thanks!
Last edited on
After you pull data from the buffer it is "empty", yes it's true that physically there is still data in the memory there but as a general rule of computer science we functionally ignore quarks like this. Kind of like how we ignore that files aren't really deleted when you empty your recycle bin. It's up to the operating systems schedular to clear memory when a program exits (Warning that statement WILL start a flame war in certain circles), the only real obligation you have to the user is to ensure you don't leave any orphan threads in memory that the OS might think are still open.

So "to be frank" WTF (weak pun intended) are you trying to accomplish?
Last edited on
well anyways I've read the documentation for seekg and it states that it is a member of an istream object, and cin is an istream object, yet the above getsize() code works for fstreams, it doesn't work with cin, so what I'm trying to accomplish is find out why it is not working.
Topic archived. No new replies allowed.