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;
}
// istream::unget example
#include <iostream> // std::cin, std::cout
#include <string> // std::string
usingnamespace 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;
}
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