【问题标题】:what is the problem in my file handling program?我的文件处理程序有什么问题?
【发布时间】:2020-07-23 12:40:34
【问题描述】:

我写了以下代码

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

我做错了什么?

【问题讨论】:

    标签: c++ file-handling


    【解决方案1】:

    问题是第 4 行的“accountnumber[20]”是一个整数数组,当它需要一个 char 数组时,你将它传递给第 10 行的 cin.getline。由于您将 char 数组用于余额等数值,因此您可能需要考虑将您的帐号也设为 char 数组。

    int accountnumber[20];
    char firstname[20], 
    lastname[20], balance[20];
    

    变成

    char accountnumber[20], firstname[20], lastname[20], balance[20];
    

    您的代码格式也非常规,您可能希望坚持使用更常见的内容,以使任何人都更容易阅读。

    【讨论】:

    • 好吧,格式不仅非常规,恐怕它只会使代码在语法上无效。
    • 或者可以使用int 来代替任何数组。
    • 我当然会使用数字类型来表示余额,但至于它所依赖的帐号,在某些标识符中,前导零是有效的,数字类型会简单地省略这些,而 int 会在大多数系统上无法保存 20 位数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多