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( )を割り当てようとしていますが、これは失敗する運命にあります。balancexstd::stringuser

TL;DR userは。として宣言されていませんstruct bankUser

于 2012-06-14T19:06:28.880 に答える