我编写了以下代码
switch (number) {
case 1:
int accountnumber[20];
char firstname[20], lastname[20], balance[20];
cout << "please enter the account number of the user " << endl;
cin.getline(accountnumber, 20);
cout << "please enter the first name of the user " << endl;
cin.getline(firstname, 20);
cout << "please enter the last name of the user " << endl;
cin.getline(lastname, 20);
cout << "please enter the balance of the user " << endl;
cin.getline(balance, 20);
ofstream myfile(" data.txt");
myfile << accountnumber;
myfile.close();
int accountnumber1[20];
ifstrean.obj("data.txt");
obj.getline(accountnumber1, 20);
cout << "data is" << accountnumber1;
obj.close();
}
它显示以下错误
no matching function for call to 'std::basic_istream<char>::getline(int[20], int) '
expected unqualified-id before'.' token
我犯了什么错误?
问题是第4行的“AccountNumber[20]”是一个整数数组,您将它传递给第10行的Cin.getLine,而它需要一个char数组。 由于您使用char数组来处理诸如balance之类的数值,因此您可能需要考虑将您的帐号也变成一个char数组。
int accountnumber[20];
char firstname[20],
lastname[20], balance[20];
成为
char accountnumber[20], firstname[20], lastname[20], balance[20];
您的代码的格式也是非常非传统的,您可能想要坚持一些更常见的东西,以使任何人都更容易阅读它。