c++ - 结构错误 : Request for member

标签 c++ struct compiler-errors

我正在制作一个程序来保存银行信息(即帐户、密码、余额)。我遇到了某个错误,但我不确定原因。

这是完整的代码。

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int x = 0;

void addUser();
void login();
void deposit();
void withdrawl();

struct bankUser
{
    double balance;
    string account, password;
};

bankUser user[20];

int menu()
{
    ofstream myfile("bankmachine.txt", ios::app);
    char choice;

    cout << "1) Add account" << endl;
    cout << "2) Log in" << endl;
    cout << "3) Make deposit" << endl;
    cout << "4) Make withdrawl" << endl;
    cout << "5) Quit" << endl << endl;
    cout << "What would you like to do?: ";
    cin >> choice;
    cout << endl;

    switch (choice)
    {
        case '1':
            addUser();
            break;
        case '2':
            login();
            break;
        case '3':
            deposit();
            break;
        case '4':
            withdrawl();
            break;
        case '5':
            myfile << user[x].balance << endl;
            myfile.close();

            cout << "Thank you for using the banking system." << endl << endl;
            system ("pause");
            return 0;
            break;
        default:
            cout << "That is not a valid option. Please choose again." << endl << endl;
            menu();
    }
}

void addUser()
{
    if ((x >= 0) && (x < 20))
    {
        cout << "Please enter your desired account name: ";
        cin >> user[x].account; // Account name.
        cout << "Thank you, now please enter your desired password: ";
        cin >> user[x].password; // Account password.
        cout << "\nAccount created. You may now log in." << endl << endl;

        ofstream myfile("bankmachine.txt", ios::app); // Opens the text file at the end of the file (if data is already present).
        myfile << user[x].account << endl; // Writes to text file.
        myfile << user[x].password << endl; //  ^
        myfile.close(); // Close text file (important).

        x++; // Increases to simulate the addition of another user.
        menu();
    }

    else // Will display if user has entered 20 users.
    {
        cout << "You have entered the maximum number of users." << endl;
        menu();
    }
}

void deposit()
{
    int deposit;
    string answer;

    do
    {
        cout << "Please enter the amount of money that you would like to deposit: ";
        cin >> deposit;

        user[x].balance += deposit;

        cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
        cout << "Would you like to make another deposit? (Y/N): ";
        cin >> answer;
    } while ((answer != "N") && (answer == "Y"));
    cout << endl;
    menu();
}

void withdrawl()
{
    int withdraw;
    string answer;

    do
    {
        cout << "Please enter the amount of money that you would like to withdraw: ";
        cin >> withdraw;

        if (withdraw <= user[x].balance)
        {
            user[x].balance -= withdraw;
        }

        else
        {
            cout << "\nSorry, you do not have sufficient funds to complete this withdrawl.\nPlease try again." << endl << endl;
            withdrawl();
        }

        cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
        cout << "Would you like to make another withdrawl? (Y/N): ";
        cin >> answer;
    } while (answer != "N" && answer == "Y");
    cout << endl;
    menu();
}

void login() // Function to log in.
{
    string user, pw, usernameCheck, passwordCheck;
    double balance;

    cout << "Please enter your login information." << endl << endl;
    cout << "Account name: ";
    cin >> user;
    cout << "Password: ";
    cin >> pw;
    cout << endl;

    ifstream myfile("bankmachine.txt", ios::app);

    while (!myfile.eof()) // Loops until end of file.
    {
        getline(myfile, usernameCheck);
        if (usernameCheck == user)
        {
            getline(myfile, passwordCheck);
            if (passwordCheck == pw)
            {
                myfile >> balance;
                cout << "Login successful." << endl;
                cout << "Your balance is " << balance << "." << endl << endl;

                user[x].balance = balance;
            }

            else // If not, display:
            {
                cout << "Password incorrect." << endl << endl;
            }
        }
    }

    myfile.close(); // Close text file (important).
    menu();
}

int main()
{
    cout << "Welcome to the banking system." << endl << endl;
    menu();
}

我不断收到此错误(第 172 行):

request for member 'balance' in 'user.std::basic_string<_CharT, _Traits,
_Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)x))', which is of non-class type 'char'|

这是什么原因造成的?我该如何解决?任何答案表示赞赏。

最佳答案

根据提供的错误判断,user 似乎不是 struct bankUser 类型,而是 std::string

您正在尝试将 std::string(balance)分配给 std::的偏移量 x 处的字符: string 命名为 user,这是注定要失败的。

TL;DR user 未声明为 struct bankUser

关于c++ - 结构错误 : Request for member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039694/

相关文章:

c - 如何从二进制文件写入和读取动态数组

java - 转换HH :MM:SS (AM/PM) into Seconds的String时间格式

java - 循环体后找不到符号

c++ - 提供特征类的类型的模板特化

c++ - 实现 std::hash<T> 时修改内部结构

c++ - 如何使用 CMake 在特定构建配置中为特定目标设置特定编译器标志?

c - 为什么修改数据字段会改变链接列表中的其他字段

c++ - C++中 "using"关键字背后的逻辑是什么?

c++ - struct Node* 和 Node* 在函数调用上有什么区别?

Ccrabble C 程序编译但不允许用户执行该文件