开发者

Output and Input at Same Line using IOSTREAM

开发者 https://www.devze.com 2023-03-18 16:05 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号