I\'m writing a c++ program and I\'m not sure why it\'s not working. #include <io
ID: 3928147 • Letter: I
Question
I'm writing a c++ program and I'm not sure why it's not working.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Howard_COP2513_F1601 {
void moneyOption();
void bankOption();
void login();
void createAcnt();
void quit();
void dMoney(); // Deposit money
void wMoney(); // Withdraw money
void rBalance(); // Request balance
char userInput = '?';
char userInput2 = '?';
string id = "?";
string password = "?";
string ID = "?";
string PASSWORD = "?";
double DEPOSIT = 0.00;
double WITHDRAW = 0.00;
double OLDBALANCE = 0.00;
double NEWBALANCE = 0.00;
int main() {
bankOption();
return 0;
}
bankOption() {
cout << "Please select an option: " << endl;
cout << "l -> Login " << endl;
cout << "c -> Create New Account " << endl;
cout << "q -> Quit " << endl;
cin >> userInput;
if (userInput == 'l' || 'L') {
login();
}
else if (userInput == 'c' || 'C') {
createAcnt();
}
else if (userInput == 'q' || 'Q') {
quit();
else {
cout << "Invalid Input. " << endl;
main();
}
return 0;
}
}
moneyOption() {
cout << "d -> Deposit Money " << endl;
cout << "w -> Withdraw Money " << endl;
cout << "r -> Request Balance" << endl;
cin >> userInput2;
if (userInput2 == 'd' || 'D') {
dMoney();
}
else if (userInput2 == 'w' || 'W') {
wMoney();
}
else if (userInput2 == 'r' || 'R') {
rBalance();
}
else {
cout << "Invalid Input. " << endl;
moneyOption();
}
return 0;
}
login() {
cout << "Please enter your user id: " << endl;
cin >> ID;
cout << "Please enter your password: " << endl;
cin >> PASSWORD;
if (id == ID && password == Password) {
cout << "Access Granted - " << ID << endl;
}
else {
cout << "******** " << "LOGIN FAILED! " << "********" << endl;
bankOption();
}
moneyOption();
return 0;
}
createAcnt() {
cout << "Please enter your user name: " << endl;
cin >> ID;
cout << "Please enter your password: " << endl;
cin >> PASSWORD;
bankOption();
return 0;
}
quit() {
cout << "Thanks for banking with COP2513.F16,"
system("pause");
return 0;
}
dMoney() {
cout << "Amount of deposit: " << endl;
cin >> DEPOSIT;
OLDBALANCE = NEWBALANCE;
NEWBALANCE = BALANCE + DEPOSIT;
moneyOption();
return 0;
}
wMoney() {
cout << "Amount of withdrawal: " << endl;
cin >> WITHDRAW;
OLDBALANCE = NEWBALANCE;
NEWBALANCE = BALANCE - WITHDRAW;
moneyOption;
return 0;
}
rBalance() {
if (DEPOSIT > 0) {
cout << "Beginning balance: " << "$ " << fixed << setprecision(2) << OLDBALANCE << endl;
cout << "Deposit amount: " << "$ " << fixed << setprecision(2) << DEPOSIT << endl;
cout << "Your Balance is: " << "$ " << fixed << setprecision(2) << NEWBALANCE << endl;
}
else if (WITHDRAW > 0) {
cout << "Beginning balance: " << "$ " << << fixed << setprecision(2) << OLDBALANCE << endl;
cout << "Withdrawal amount: " << "$ " << << fixed << setprecision(2) << WITHDRAW << endl;
cout << "Your Balance is: " << "$ " << fixed << setprecision(2) << NEWBALANCE << endl;
return 0;
}
}
}
Explanation / Answer
Ive modified the code to remove errors. Have a look.
PROGRAM CODE with corrections and comments
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Ive removed the function prototypes, modified them with void return type and removed the return statement
class Howard_COP2513_F1601 {
public:
// Request balance
char userInput = '?';
char userInput2 = '?';
string id = "?";
string password = "?";
string ID = "?";
string PASSWORD = "?";
double DEPOSIT = 0.00;
double WITHDRAW = 0.00;
double OLDBALANCE = 0.00;
double NEWBALANCE = 0.00;
//changed the brackets for this function
void bankOption() {
cout << "Please select an option: " << endl;
cout << "l -> Login " << endl;
cout << "c -> Create New Account " << endl;
cout << "q -> Quit " << endl;
cin >> userInput;
if (userInput == 'l' || 'L') {
login();
}
else if (userInput == 'c' || 'C') {
createAcnt();
}
else if (userInput == 'q' || 'Q') {
quit();
}
else {
cout << "Invalid Input. " << endl;
// main(); program execution starts from main() . there cant be a function call for main.
}
}
void moneyOption() {
cout << "d -> Deposit Money " << endl;
cout << "w -> Withdraw Money " << endl;
cout << "r -> Request Balance" << endl;
cin >> userInput2;
if (userInput2 == 'd' || 'D') {
dMoney();
}
else if (userInput2 == 'w' || 'W') {
wMoney();
}
else if (userInput2 == 'r' || 'R') {
rBalance();
}
else {
cout << "Invalid Input. " << endl;
moneyOption();
}
}
int login() {
cout << "Please enter your user id: " << endl;
cin >> ID;
cout << "Please enter your password: " << endl;
cin >> PASSWORD;
if (id == ID && password == PASSWORD) { // changed the varaible to PASSWORD
cout << "Access Granted - " << ID << endl;
}
else {
cout << "******** " << "LOGIN FAILED! " << "********" << endl;
bankOption();
}
moneyOption();
return 0;
}
int createAcnt() {
cout << "Please enter your user name: " << endl;
cin >> ID;
cout << "Please enter your password: " << endl;
cin >> PASSWORD;
bankOption();
return 0;
}
int quit() {
cout << "Thanks for banking with COP2513.F16,"; // semicolon was missing
system("pause");
return 0;
}
int dMoney() {
cout << "Amount of deposit: " << endl;
cin >> DEPOSIT;
OLDBALANCE = NEWBALANCE;
NEWBALANCE = OLDBALANCE + DEPOSIT; // changed the variable name to OLDBALANCE
moneyOption();
return 0;
}
int wMoney() {
cout << "Amount of withdrawal: " << endl;
cin >> WITHDRAW;
OLDBALANCE = NEWBALANCE;
NEWBALANCE = OLDBALANCE - WITHDRAW; // changed the variable name to OLDBALANCE
moneyOption(); // function call missing brackets
return 0;
}
int rBalance() {
if (DEPOSIT > 0) {
cout << "Beginning balance: " << "$ " << fixed << setprecision(2) << OLDBALANCE << endl;
cout << "Deposit amount: " << "$ " << fixed << setprecision(2) << DEPOSIT << endl;
cout << "Your Balance is: " << "$ " << fixed << setprecision(2) << NEWBALANCE << endl;
}
else if (WITHDRAW > 0) {
cout << "Beginning balance: " << "$ " << fixed << setprecision(2) << OLDBALANCE << endl;//typos
cout << "Withdrawal amount: " << "$ " << fixed << setprecision(2) << WITHDRAW << endl;//typos
cout << "Your Balance is: " << "$ " << fixed << setprecision(2) << NEWBALANCE << endl;
}
return 0; //moved return function outside the bracket
}
}; // Class declaration must end with semicolon
//main function should be outside of the class
//functions of the class shoudl be invoked using objects of the same class.
//Ive created one for this class
int main() {
Howard_COP2513_F1601 obj1;
obj1.bankOption();
return 0;
}