I'm trying to build an ATM program, where a user creates their account and uses the same to make deposits and withdrawal as well as check their account balance.
I've put the whole program so you can run and try it, in case I'm clear about my issue:
#include <iostream>
#include <stdlib.h>
using namespace std;
void printIntroMenu ();
void printMainMenu ();
void start ();
void login ();
void createAccount ();
char menuInput;
int user;
int password;
float a;
float totalAmount = 0.0;
int
main ()
{
cout << "Hi! Welcome to Mr. Zamar's ATM Machine! \n \n";
start ();
return 0;
}
void
printIntroMenu ()
{
switch (menuInput)
{
case 'l':
login ();
break;
case 'c':
createAccount ();
break;
case 'q':
exit (0);
break;
default:
cout << "Error! The selected option is not correct";
break;
}
}
void
printMainMenu ()
{
switch (menuInput)
{
case 'd':
cout << "Enter the amount to be deposited: ";
cin >> a;
cout << "Amount of the deposit: " << a;
break;
case 'w':
cout << "Enter the amount to be withdrawn: ";
cin >> a;
cout << "Amount of the withdrawal: " << a;
break;
case 'r':
if (menuInput == 'd')
{
totalAmount = totalAmount + a;
cout << "Your balance is : " << totalAmount;
}
else if (menuInput == 'w')
{
totalAmount = totalAmount - a;
cout << "Your balance is : " << totalAmount;
}
break;
case 'q':
exit (0);
break;
}
}
void
start ()
{
cout <<
"\n \n Please select an option from the menu below: \n l -> login \n c -> Create a new account \n q -> Quit ";
cin >> menuInput;
cout << " \n >> " << menuInput;
printIntroMenu ();
}
void
createAccount ()
{
cout << "\n\n Please enter your username: ";
cin >> user;
cout << "\n Please enter your password: ";
cin >> password;
cout << "\n Thank You! Your account has been created! \n Your username is "
<< user << " password is " << password;
start ();
}
void
login ()
{
int userid;
int pass;
cout << "\n\n Please enter your username: ";
cin >> userid;
cout << "\n Please enter your password: ";
cin >> pass;
/*
The part below in the if else doesn't execute the functions printMainMenu and start.
As soon as a user logins using the id and password, they created the program ends.
Is there some call by value or call by reference method to avoid this problem
*/
if (userid == user && pass == password)
{
cout << "\n\n **************** LOGIN SUCCESSFUL ******************\n\n";
printMainMenu ();
}
else
{
cout << "\n\n **************** LOGIN FAILED ****************** \n\n";
start ();
}
}
You can not store a user name or password in a integer. Use
std::string user;orchar [100] user;This will not work for string comparison:
Change your code to use string comparison functions for
char [].or for
std::stringYou input will work but can be improved with
cin.get(user, 100);orgetline(cin, user);