What does in_file.unget do in this case?

Im trying to understand what the in_file.unget does.

In my text, it says "it can be used to see if the next value is numeric or text"
So i tried this example i found on the net, but it isn't running.
It keeps saying: 'in_file' : undeclared identifier
Am I writing the code wrong?

Thanks

1
2
3
4
5
6
7
8
9
10
11
  cout << "Enter current month (Jan, Feb, Mar etc.) and year " << endl;
	char ch;
	cin.get(ch);
	if (isdigit(ch))
	{
		in_file.unget();

		int n;
		cin >> n;
		cout << n << endl;
	}
Last edited on
never used unget before myself but maybe this will help
http://www.cplusplus.com/reference/istream/istream/unget/?kw=unget
Thanks, i read through it, but I still dont understand it. And the example is a bit hard to follow
bump
after you get a character from the user. You can use unget to put that character back into the input stream, so it can be extracted a second time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// istream::unget example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::string
using namespace std;

int main () 
{
	
	char n,c;
	cout << "Please, enter a character ";
	c = cin.get();//<<----------------------------extracts your character from the input stream
	

	cin.unget(); //<<----------------------------------puts the character the user entered back in the input stream
	cin >> n;    //<<----------------------------------extracts the character from the input stream a second time
	cin.ignore();	
	
	cout << "Your characters are " << n <<" and " << c << '\n';

	cin.ignore(100, '\n');
	return 0;
}
ok thanks, so theoretically if i were to input MAY 2013 into the code I have above, the output would be the same as the input?
if you entered MAY 2013 in the code above there would be no output because line 6 if (isdigit(ch)) would evaluate to false and the if statement wouldn't execute.

unget only works on one character so if you extract the M from MAY 2013 you would have this AY 2013. Then if you call unget you would put the M back in the stream. So you would have MAY 2013 again
Topic archived. No new replies allowed.