0

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

这是整个代码。

#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'|

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

4

1 回答 1

3

从提供的错误来看,它似乎user不是类型struct bankUser,而是std::string.

您正在尝试将std::string( ) 分配给您namedbalance的偏移量处的字符,这注定会失败。xstd::stringuser

TL;DR user未声明为struct bankUser.

于 2012-06-14T19:06:28.880 回答