0
私はatmマシンを構築するためにこの学校の割り当てを受けました。私はそれが動作するようにしたが、私はお金を預金または引き出し、私の口座残高を見てもまだ更新されていない。誰かが私が間違ってやっていることを教えてもらえますか、私のコードを良くする方法についての提案は、一般に認められます。C++は自分の変数の1つを更新できません
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
void WelcomeMenu();
void Menu();
void userChoiceDeposite(double accountbalance);
void userChoiceWithdraw(double accountbalance);
void userChoiceView(double accountbalance);
void printReceipt();
int main()
{
//clear receipt.txt from previous transactions
ofstream clearFile("Receipt.txt");
clearFile.close();
//Declaring variables
string userInput;
char userAction;
//prompt user for input, start the program or quit
cout <<"Enter start to continue, or Q/q to quit ";
getline (cin, userInput);
cout <<endl;
//check the input
if ((userInput == "q") || (userInput == "Q"))
{
cout <<"Have a nice day!" <<endl;
return 0;
}
else if (userInput == "start")
{
const int sizeLimit = 50;
string firstName[sizeLimit], lastName[sizeLimit], passWord[sizeLimit];
double accountBalance[sizeLimit];
int count = 0;
ifstream readFile("Accounts.txt");
while (!readFile.eof())
{
readFile >> firstName[count];
readFile >> lastName[count];
readFile >> passWord[count];
readFile >> accountBalance[count];
count++;
}
//ask for username/pw
string fName, lName, pwd;
double accountbalance;
bool exists = false;
do
{
cout <<"Enter First Name: ";
cin >> fName;
cout <<"Enter Last Name: ";
cin >>lName;
cout <<"Enter Password: ";
cin >> pwd;
for (int i=0; i<sizeLimit; i++)
{
if (firstName[i] == fName)
{
if (lastName[i] == lName)
{
if (passWord[i] == pwd)
{
exists = true;
accountbalance = accountBalance[i];
}
}
}
}
}
while (exists == false);
//if correct display menu
WelcomeMenu();
cin >> userAction;
//while login is valid (true)
while (userAction != 'q')
{
if (userAction == 'D' || userAction == 'd')
{
userChoiceDeposite(accountbalance);
}
else if (userAction == 'W' || userAction == 'w')
{
userChoiceWithdraw(accountbalance);
}
else if (userAction == 'V' || userAction == 'v')
{
userChoiceView(accountbalance);
}
else if (userAction == 'P' || userAction == 'p')
{
printReceipt();
return 0;
}
else
{
cout <<"Have a nice Day" <<endl;
return 0;
}
Menu();
cin >> userAction;
}
}
else
{
cout <<"Input Error..." <<endl;
}
return 0;
}
//welcome menu display function
void WelcomeMenu()
{
cout <<"Welcome to your account " <<endl;
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[Q/q] Quit" <<endl;
}
//function after action was taken (add print option)
void Menu()
{
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[P/p] Print Receipt and quit" <<endl;
cout <<"[Q/q] Quit" <<endl;
}
//deposit function
void userChoiceDeposite(double accountbalance)
{
int depositMoney;
cout <<"Enter the ammount of money you want to deposit. Max $10000" <<endl;
cin >> depositMoney;
if ((depositMoney > 0) && (depositMoney <= 10000))
{
accountbalance = accountbalance + depositMoney;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You made a deposit of $" <<depositMoney <<endl;
saveAction.close();
}
else
{
cout <<"Incorrect ammount" <<endl;
return userChoiceDeposite(1);
}
}
//withdraw function
void userChoiceWithdraw(double accountbalance)
{
int withdrawMoney;
cout <<"Enter the ammount of $ you want to withdraw:";
cin >> withdrawMoney;
if (accountbalance < withdrawMoney)
{
cout <<"You dont have that much money"<<endl;
return userChoiceWithdraw(1);
}
else
{
accountbalance = accountbalance - withdrawMoney;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You withdrew $" <<withdrawMoney <<endl;
saveAction.close();
}
}
//view function
void userChoiceView(double accountbalance)
{
cout <<"Your account balance is $" <<accountbalance <<endl;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You viewed your account balance" <<endl;
saveAction.close();
}
//print receipt function
void printReceipt()
{
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "Thank you! Come again!" <<endl;
saveAction.close();
}
related/dupe:https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – NathanOliver
また読む価値がある:[なぜiostream: :ループ状態の内部のeofが間違っていると思う](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) 'while(readFile >> firstName [count] >> lastName [count] >> passWord [count] >> accountBalance [count]){ count ++; } 'これは、' >> 'を利用して、入力ストリームへの参照を返し、IOと[' operator bool']の連鎖を許可します(http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool ) – user4581301
[参照渡しと値渡しの違いは何ですか?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing -by-value) – perfect5th