Balance returning negative ammount

Hello Everyone!
I have this homework on school where i have to create a ATM machine and users have to register and login. I created the login and register menu and it works good but the problem is i wana create more users and save them as .txt file and save his deposit info.
For ex ; username: John Pasword: 1234 Money: 2345$ and everytime i login with his username and pasword i have the amount of money he saved. And another problem that im occuring on this code is that when i deposit and i withdraw money it returns negative ammount or 0 money and it doesnt execute the error message when i try to withdraw with 0 money on my back account.

This is my code

#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;

class User {

public:
string username;
string password;
string inputUsername;
string inputPassword;
float balance = 0.00;
float withdrawDeposit = 0.00;
float number = 0.00;
int userChoice;
float deposit = 0.00;
float withdraw = 0.00;


// User registration
void userRegisterDo()
{

ofstream usersFile ("userData1.txt");

if ( !usersFile.is_open())
{
usersFile.open("userData1.txt");
}

usersFile << username << " " << password <<endl;

usersFile.close();
}

// Initialize user registration
void userRegister()
{
cout << "Welcome!\n-------------------------\n\nPlease register.\nEnter a new username:\n";
cin >> username;

cout << "\nPlease enter a new password:\n";
cin >> password;

userRegisterDo();
}


// User login function
void login()
{
cout << "Please enter your username:\n";
cin >> inputUsername;

cout << "\nPlease enter your password:\n";
cin >> inputPassword;

string userAndPass = inputUsername + " " + inputPassword; // Search pattern
int offset;
string line;
ifstream usersFile;
usersFile.open ("userData1.txt");

if(usersFile.is_open())
{
bool found = false;
if(usersFile.is_open()) {
while(getline(usersFile,line) && !found) {
if (line.compare(userAndPass) == 0) { //match strings exactly!
found = true; // found is true => break loop
}
}
usersFile.close(); //close the file before recursivly opening it again later
if(found) {

userInterface();
}
else {
cout << "\nUsername and/or password incorrect!\n\n\n\n";
login(); //ok we didn't find them, lets redue this!
}
}

usersFile.close();
}
else
cout << "Unable to open userData.txt file." << endl;
}




void atmMainMenu()
{
cout << "-------------------------------------------\n";
cout << " CONE Bank ATM machine\n";
cout << "-------------------------------------------\n" << endl;
cout << "Please select an option from the menu below: \n";
cout << "1. Login\n";
cout << "2. Create a New Account\n";
cout << "3. Quit\n" << endl;
cout << "Your Choice: ";
cin >> userChoice;



//Validates the user's input & executes function calls.
switch (userChoice)
{
// Case 1 - Displays the login menu.
case 1:
login();
break;

// Case 2 - Creates a user name & password.
case 2:
userRegisterDo();
userRegister();
atmMainMenu();
break;

// Case 3 - The Program Exits.
case 3:
cout << "-------------------------------------------";
cout << "\nThank You for using our ATM!\n";
cout << "\n CONE BANK !\n";
cout<< "-------------------------------------------";
exit(0);
break;

// TEST ERROR MESSAGE.
default:
cout << "INTERNAL ERROR" << endl;
}

}



void userInterface()
{


cout << "Welcome "<< inputUsername << '\n';
cout << "-------------------------------------------\n";
cout << " Action Menu\n";
cout << "-------------------------------------------\n" << endl;
cout << "1. Deposit Money\n";
cout << "2. Withdraw Money\n";
cout << "3. Request Balance\n";
cout << "4. Quit\n";
cout << "Your Choice : ";
cin >> userChoice;


// Validates the user's input
switch(userChoice)
{
// Case 1 - Requests a deposit amount from the user.
case 1:
balance = deposit;
cout << "Enter the amount of money to deposit: $ ";
cin >> withdrawDeposit;
cout << "\nDeposit successful! ";
cout << endl;
balance += withdrawDeposit;

userInterface();

break;

// Case 2 - Requests the withdraw amount.
case 2:
balance = withdraw;
cout << "Enter the amount of money to withdraw: "<<balance<<" $ ";
cin >> number;


userInterface();
balance -= number;

// Validates whether the user has enough funds to execute a withdraw.
while (balance < 0.00)
{
// Displays an error if balance is a negative value.
// Also sends the user back to the action menu.
int userChoice = 0;
cout << "\n **ERROR** - Withdraw amount.\n";
cout << "You do not have enough funds to withdraw that amount.\n";
cout << endl;
cout << "Choose from the menu again.\n";
cout << endl;
balance += number;

userInterface();
}

//Returns the balance value if balance is not a negative value.
cout << "\nWithdraw successful! ";
cout << endl;
break;

// Case 3 - Displays the user's current balance.
case 3:
cout << "\nYour Balance is: "<<balance<<"$";
cout << endl;
userInterface();
break;


// Case 4 - Quit Program
case 4:
userChoice = 0;
cout << "-------------------------------------------\n";
cout << "\nThank You for using our ATM!\n";
cout << "\n CONE BANK\n";
cout << "-------------------------------------------\n";
cout << endl;
exit(0);
break;

// TEST ERROR MESSAGE.
default:
cout << "ERROR: Pick from 1 to 4!";
}
}
};

// Main program
int main() {

User user1;

user1.atmMainMenu();
ifstream usersFile("userData1.txt");
long begin, end;

if (usersFile.good())
{
cout << "File userData.txt found!\n\n";
}

else {
user1.userRegister();
}

if(usersFile.is_open())
{
begin = usersFile.tellg();
usersFile.seekg (0, ios::end);

end = usersFile.tellg();


if(begin == end)
{
user1.userRegister();
}

else
{
user1.login();
}
}

getch();

}
It's too hard to read all that code with no indentation.
it can be made to work with weird variable name usage, but it would be much easier to debug if you used the real English terms correctly.

// Case 1 - Requests a deposit amount from the user.
case 1:
balance = deposit;
cout << "Enter the amount of money to deposit: $ ";
cin >> withdrawDeposit;
cout << "\nDeposit successful! ";
cout << endl;
balance += withdrawDeposit;
userInterface();


balance: what is in the account at the start of a transaction.
here, it is being assigned deposit, which makes no sense in English.
withdrawDeposit -- which is it? withdrawals take money out of the balance. deposits put money into the balance.
deposit is then said to be successful, but no actions have happened yet. very optimistic.
then balance, which is deposit?, is modified.


whiskey tango foxtrot man?

I would expect..
tmp = balance;
cout << "Enter the amount of money to deposit: $ ";
cin >> deposit;
balance += deposit;
if(balance-tmp == deposit)//or some other similar check
cout << "\nDeposit successful! ";
else
cout << "\nDeposit unsuccessful! ";


or similar code, using the terms as one would in a conversation about bank accounts.

Last edited on
Topic archived. No new replies allowed.