Output and Input at Same Line using IOSTREAM
I want to use the iostream input and output operators in the same statement, not to be nicer to the user but the look I was trying not successfully obtained.
Code fragment:
int value = 0;
std::cout << "Number 1: " << std::cin >> value << std::endl;
Is there any way to开发者_高级运维 do this using only cin cout?
struct IO {
template <typename T>
const IO & operator << (const T & t) const {
std :: cout << t;
return *this;
}
template <typename T>
const IO & operator >> (T & t) const {
std :: cin >> t;
return *this;
}
};
IO () << "Number 1: " >> value;
a bit messy but I think this is what you wanted
std::cout<<"Data : "<<val<<std::endl<<(std::cin>>val)<<"\r"<<"\t\r\n"<<std::flush;
std::cout << "Number 1: ";
std::cin >> value;
Should do the trick.
精彩评论