C++ 为银行程序计算读/写的问题

C++ 为银行程序计算读/写的问题,c++,login,banking,C++,Login,Banking,我正在尝试制作一个银行程序,可以从文本文件中读取用户名和密码,并将其与用户输入的内容进行比较。我已经搜索并尝试了几种不同的方法,但似乎无法使其发挥作用。我很抱歉,如果这是一个重新发布,我只是不明白。 我对编码非常陌生,所以请原谅我的错误 我的文本文件名为:“Account.txt” 它在第一行包含“username”,然后在第二行包含“userpassword” /* Hunter Walker 11/10/2015 Banking Application with Input validati

我正在尝试制作一个银行程序,可以从文本文件中读取用户名和密码,并将其与用户输入的内容进行比较。我已经搜索并尝试了几种不同的方法,但似乎无法使其发挥作用。我很抱歉,如果这是一个重新发布,我只是不明白。 我对编码非常陌生,所以请原谅我的错误

我的文本文件名为:“Account.txt” 它在第一行包含“username”,然后在第二行包含“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;
}
/*
亨特·沃克2015年10月11日
具有输入验证的银行应用程序
任务2
*/
//标题
#包括
#包括
#包括
#包括
使用名称空间std;
//变数
字符输入1,输入2;
字符串username、userpassword、newusername、newuserpassword;
字符串customuser,custompass;
双阿蒙坦,阿蒙托;
双倍合计=0;
//函数声明
int bankingmenu();
int newaccount();
int login();
int main();
int main菜单();
int存款();
int-draw();
int showbalance();
//要从文件中读取的代码
//银行应用程序的主菜单
int主菜单()
{

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

ofstream outfile;
outfile.open("Account.txt");
outfile<<newusername<<endl;
outfile<<newpassword<<endl;
outfile.close();
这将解决当前获取单个用户的用户名和密码的问题。但是,您仍然需要找到一种方法来获取将使用您的应用程序的多个用户的用户名和密码

处理多个用户的一种更简单的适应方法是简单地将用户名和密码附加到文件中,然后按顺序搜索用户名和密码

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

inputFile>>customuser;
inputFile>>custompass;