c++输入流不等待提取运算符重载的输入

发布于 2024-09-05 04:35:52 字数 337 浏览 4 评论 0原文

这个问题让我很烦恼。它不会等待输入,而是关闭。我已经尝试解决这个问题有一段时间了。有什么想法吗?

    istream& operator>>(istream& is, Account &a) {
    cout << "Enter an accoutn number: ";
        is >> a.accountNo;
        cout << endl;
    cout << "Balance: ";
        is >> a.bal;
    return is;
    }

This problem is annoying me. Instead of waiting for input, it just closes. I've been trying to figure this out for a while now. Any ideas?

    istream& operator>>(istream& is, Account &a) {
    cout << "Enter an accoutn number: ";
        is >> a.accountNo;
        cout << endl;
    cout << "Balance: ";
        is >> a.bal;
    return is;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-09-12 04:35:52

当我将其放入以下程序时,它工作得很好(尽管如果您尝试从 std::cin 之外的任何内容读取帐户,它就不会工作得那么好):

#include <iostream>

struct Account { 
    int accountNo;
    int bal;
};

using std::ostream;
using std::istream;
using std::cout;
using std::endl;

istream& operator>>(istream& is, Account &a) {
    cout << "Enter an account number: ";
    is >> a.accountNo;
    cout << endl;
    cout << "Balance: ";
    is >> a.bal;
    return is;
}

ostream &operator<<(ostream &os, Account const &a) { 
    return os << "Account #: " << a.accountNo << "\tBalance: " << a.bal;
}

int main() { 
    Account a;
    std::cin >> a;

    std::cout << a;
    return 0;
}

When I put it into the following program, it worked fine (though it wouldn't work so well if you tried to read an account from anything but std::cin):

#include <iostream>

struct Account { 
    int accountNo;
    int bal;
};

using std::ostream;
using std::istream;
using std::cout;
using std::endl;

istream& operator>>(istream& is, Account &a) {
    cout << "Enter an account number: ";
    is >> a.accountNo;
    cout << endl;
    cout << "Balance: ";
    is >> a.bal;
    return is;
}

ostream &operator<<(ostream &os, Account const &a) { 
    return os << "Account #: " << a.accountNo << "\tBalance: " << a.bal;
}

int main() { 
    Account a;
    std::cin >> a;

    std::cout << a;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文