我是一名程序员,学习 C++ 一段时间了,我似乎遇到了一个我无法用自己的知识解决的问题。
每次运行 for 循环时,我的控制台都会在未完成脚本的情况下退出,无论如何我可以解决这个问题吗?
class Account {
public:
string fname;
string lname;
string pin;
int money;
};
do
{
cin >> input;
if (input == "Login" || input == "login")
{
int found;
do {
cout << endl << "Please insert your PIN" << endl; cin >> input;
for (i = 0; i < sizeof(account); i++) //Process exits right after I send input. {
if (input == account[i].pin)
{
LoggedIn(account[i]);
found++;
} else if (found <= 1) {
cout << endl << "Your PIN fails to match your account, please try again.";
}
}
}
老实说,我无能为力或不知道可以解决这个问题
sizeof(account)
不会为您提供 Account 对象数组中的元素数量,您可以使用 sizeof(account) / sizeof(account[0])
,但我要做的只是使用向量,所以像这样:
vector<Account> accounts;
阅读更多关于向量的内容,它们真的很有用。
至于我要更改的代码,这里有一些:
if
语句中使用向量:for (size_t i = 0; i < accounts.size(); i++) {}
if (input == "Login" || input == "login") {
// ...
} else {
cout << "Invalid input." << endl;
}
int found = 0;