You can do this:
#include <iostream> #include <string> #include <limits> template<class T> struct Line{ T value; std::string name; }; template<class T> std::istream& operator>>(std::istream &is, Line<T> &line){ is >> line.value; is.ignore(std::numeric_limits<std::streamsize>::max(), '*'); is >> line.name; return is; } int main(){ Line<double> line; std::cin >> line; std::cout << line.name << " " << line.value; }
In the main
function, I read the value from std::cin
, but you can use any input stream std::sstream
, std::fstream
or even some custom implementation.
UPD . If suddenly you need only the first number, you can do this:
#include <iostream> #include <limits> template<class T> T get(std::istream &is){ T result; is >> result; is.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return result; } int main(){ double d; d = get<double>(std::cin); }
The get
function reads the first value, and skips the string to the end. Again, instead of std::cin
you can use std::sstream
or std::fstream