2010-06-12 72 views
-1

这个问题令我烦恼。而不是等待输入,它只是关闭。我一直试图弄清楚这一点。有任何想法吗?C++输入流不等待输入提取运算符过载

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

哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟!哎哟! 不要在您的istream运算符中执行“cout”输出。那简直是邪恶。它让我头痛。 – 2010-06-12 15:56:58

+0

你应该告诉我们你在哪里使用这个。它是'cin >> an_account;'?你能告诉我们更多的代码吗? – 2011-05-24 19:47:34

回答

1

当我把它改成下面的程序,它工作得很好(虽然它不会工作这么好,如果你想从什么,但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; 
}