hasNext Function in C++

Aug 30, 2009 at 2:19am
Hi,,,


I am aware of hasNext functions provided in Java language with class Scanner (java.util.Scanner) such as hasNextInt(), hasNextDouble(), etc..


And I am wondering if there is any similar standard/nonstandard class in C++ that's working directly with the input stream to check the type of the next potential input in the input stream buffer ???


Example:-

Standard Input Stream (cin): 1 2 3 4 5 F 7 8 9
while( cin.hasNextInt() )
cout<<"Integer";
Aug 30, 2009 at 2:59am
This is what I'd do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
#include <string>

int main(){
	std::string a,line;
	std::getline(std::cin,line);
	std::stringstream stream(line);
	while (!stream.eof()){
		stream >>a;
		std::cout <<a<<std::endl;;
	}
	std::cout <<"<end>"<<std::endl;;
	return 0;
}
Aug 30, 2009 at 10:28am
There's no such thing in the C++ standard libraries, but it's straight forward enough to build such a layer on top of std::ostream.
Topic archived. No new replies allowed.