我正在尝试构建一个ATM程序,用户可以在其中创建自己的帐户,并使用该帐户进行存款和取款以及检查帐户余额。
我已经放了整个程序,这样你就可以运行并尝试它,以防我清楚我的问题:
#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 << "nn 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 << "nn 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 << "nn **************** LOGIN SUCCESSFUL ******************nn";
printMainMenu ();
}
else
{
cout << "nn **************** LOGIN FAILED ****************** nn";
start ();
}
}
int user;
int password;
您不能将用户名或密码存储在整数中。使用std::string user;
或char [100] user;
这将不适用于字符串比较:
if (userid == user && pass == password)
更改代码以使用char []
的字符串比较函数。
if ((strcmp(userid , user) == 0) && (strcmp(pass, password) == 0))
或用于std::string
if (userid.compare(user) == 0)
您的输入将起作用,但可以使用cin.get(user, 100);
或getline(cin, user);
进行改进