我是C ++的新手,但io遇到了麻烦。输出省略了Account Number:accountNumber这一行,我也不知道为什么。
这是我的显示功能。
void displayColumnTitle()
{
int accountNumber, balanceDue;
string name;
cout << "Enter Account Number(-1 to terminate the input):" << endl;
cin >> accountNumber;
cout << "Enter name: ";
cin >> name;
cout << "Enter balance due:" << endl;
cin >> balanceDue;
cout << "Account Number: " + accountNumber << endl
<< "Name: " + name << endl << "MONTH BALANCE DUE" << endl;
}
最佳答案
"Account Number: " + accountNumber
不会将
accountNumber
连接到字符串。而是执行指针算术运算。如果accountNumber
是10
,"Account Number: " + accountNumber
相当于
"mber: "
我敢打赌这不是您所期望的。
代替
cout << "Account Number: " + accountNumber << endl
<< "Name: " + name << endl << "MONTH BALANCE DUE" << endl;
采用
// ||
// vv Use << not +
cout << "Account Number: " << accountNumber << endl
<< "Name: " << name << endl << "MONTH BALANCE DUE" << endl;
// ^^ Use << not +
// ||