计算银行计划的读/写的问题

时间:2015-11-22 06:22:07

标签: c++ login banking

我正在尝试创建一个可以从文本文件中读取用户名和密码的银行程序,并将其与用户输入的内容进行比较。我已经搜索并尝试了一些不同的方法,但我似乎无法让它工作。对不起,如果这是重新发布的帖子,我就无法弄清楚。 我对编码很陌生,所以请原谅我的错误。

我的文本文件名为:" Account.txt" 它包含"用户名"然后在第一行" userpassword"在第二行。

/*
Hunter Walker 11/10/2015
Banking Application with Input validation
Assignment 2
*/

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

using namespace std;
//Variables
char input1, input2;
string username,userpassword, newusername, newuserpassword;
string customuser, custompass;
double amountin, amountout;
double total = 0;

//Function declarations
int bankingmenu();
int newaccount();
int login();
int main();
int mainmenu();
int deposit();
int withdraw();
int showbalance();
//Code to read from file


// The main menu for the banking application
int mainmenu()
{
 cout << "Hi! Welcome to Future Computer Programmer ATM Machine!" << endl;
 cout << "Please select an option from the menu below:" << endl;
 cout << "l -> Login " << endl;
 cout << "c -> Create New Account " << endl;
 cout << "q -> Quit " << endl;
 cin >> input1;
 return 0;
}
// Function to allow the user to make a new account
int newaccount()
{
    cout << "**********************************" << endl;
    cout << "Welcome to the create an account menu!" << endl;
    cout << "Please enter your desired username:" << endl;
    cin >> newusername;
    cout << "Please enter a password for your account:" << endl;
    cin >> newuserpassword;
    cout << "Thank you for creating an account with Future Computer Programmer ATM Machine!" << endl;
    cout << "Your username is " << newusername << " and your password is " << newuserpassword << "." << endl;
    cout << endl;
    cout << "Reminder: Don't share your username or password with anyone." << endl;
    cout << endl;
    cout << "**********************************" << endl;
}
// Function to allow user to login to their account
int login()
{

    cout << "Please enter username:" << endl;
    cin >> username;
    cout << "Please enter password:" << endl;
    cin >> userpassword;
    ifstream inputFile;
    inputFile.open("Account.txt");


    cout << customuser << " " << custompass << endl;

    if (customuser == username && custompass == userpassword)
    {
        bankingmenu();
    }
    else
    {

        cout << "User name or password incorrect!" << endl;
        cout << "Returning to main menu!" << endl;
        return main();
    }
    inputFile.close();
    return 0;
}
// The secondary menu for withdrawing/depositing/ or checking balance
int bankingmenu()
{
cout << "*******************************" << endl;
cout << "Please select an option from the menu below::" << endl;
cout << " d -> Deposit Money" << endl;
cout << " w -> Withdraw Money" << endl;
cout << " r -> Request Balance" << endl;
cout << " q -> Quit" << endl;
    cin >> input2;
    if (input2 == 'd')
    {
        deposit();
    }
    else if (input2 == 'w')
    {
        withdraw();
    }
    else if (input2 == 'r')
    {
        showbalance();
    }
    else if (input2 == 'q')
    {
    cout << "Returning to main menu! " << endl;

    return main();
    }
    else
    {
        cout << "Please select a valid input and try again!" << endl;
        return bankingmenu();
    }
    return 0;

 }
// Function to allow to deposit to account
int deposit()
{
   cout << "Please enter the amount of money you wish to deposit:" << endl;
  cin >> amountin;
  total = amountin + total;
  cout << "The deposit was a success! Thanks for using Future Computer Programmer ATM Machine!" << endl;
  return bankingmenu();
}
// Function to allow user to withdraw from account
int withdraw()
{
cout << "Please enter the amount you would like to withdraw:" << endl;
cin >> amountout;
if (total < amountout)
{
cout << "You can't withdraw more money than you have!" << endl;
cout << "Please select a different amount to withdraw." << endl;
return withdraw();
}
else
{
cout << "The amount has been withdrawn." << endl;
total = total - amountout;
return bankingmenu();
}
}
// Function to display the balance
int showbalance()
{
cout << "The balance in your account is $" << total << "." << endl;
return bankingmenu();
}


// The main function that calls all previous functions to run
int main()
{

mainmenu();
// Option to login
if (input1 == 'l')
{
    login();
}
// Option to make a new account
else if (input1 == 'c')
{
    newaccount();
}
// Option to exit program
else if (input1 == 'q')
{
    cout << "Thanks for using the Future Computer Programmer ATM Machine! " << endl;
    cout << "The program will now exit!" << endl;
    return 0;
}
// Input validation
else
{
    cout << "Please select a valid menu option and try again!" << endl;
    return main();
}

return 0;
}

1 个答案:

答案 0 :(得分:0)

在newAccount函数中,您必须使用以下代码行存储用户名和密码。

ofstream outfile;
outfile.open("Account.txt");
outfile<<newusername<<endl;
outfile<<newpassword<<endl;
outfile.close();

在您的登录功能中,您必须在打开文件后添加以下两行代码&#39; Account.txt&#39;

inputFile>>customuser;
inputFile>>custompass;

这将解决目前获取单个用户的用户名和密码的问题。但是,您仍然需要找到一种方法来获取将使用您的应用程序的多个用户的用户名和密码。

更容易适应处理多个用户只需在文件中附加用户名和密码,然后按顺序搜索用户名和密码。

上述方法的替代方法是使用数据库来存储用户名和密码。