C++ issue. have to add the following: 1. When checking balance, if balance is be
ID: 3832873 • Letter: C
Question
C++ issue.
have to add the following:
1. When checking balance, if balance is below $100, the system should display the message “Low Balance” then go back to the top menu.
2. For deposit, The ATM machine validates the input by verifying that the input is positive. If not a warning message is displayed “invalid amount”, and the user is prompted to try again, if amount is negative again, the deposit operation is terminated and the top menu is displayed again. If amount is positive the Balance is updated.
3. For withdraw, if the balance is insufficient, then a warning message along with user’s balance is displayed. The user is prompted to enter a new amount for a second time, if amount is OK withdraw operation is completed and balance is updated and menu is displayed. If balance is still insufficient withdraw operation is terminated and top menu is displayed.
Im also having issues with my with my code, some extra perspective is welcome.
#include<iostream>
#include<string>
using namespace std;
class Account
{
private://store balance and id of account
int id;
double balance;
public:
Account()
{
id = 0;
balance = 1000;
}
Account(int a)
{
id = a;
balance = 1000;
}
void printId()
{
cout << "Signed in account: " << id << endl;
}
void withdraw()//define dunction to decrease the ammount in account
{
int draw;
cout << "Enter the amount to withdraw: ";
cin >> draw;
balance = balance - draw;
cout << endl;
}
void dep()//define function to deposit the amount into account
{
int dep;
cout << "Enter the amount to deposit: ";
cin >> dep;
balance = balance + dep;
cout << endl;
}
void bal()//display the account balance
{
cout << "The balance is: " << balance << endl;
cout << endl;
}
};
int main()
{
int d, ch;
Account a[10];
for (int i = 0; i < 10; i++)//use for loop to for accounts
{
Account b(i);
a[i] = b;
}
d = 10;
ch = 0;
while (true)
{
while (ch != 5)
{
cout << "Main Menu" << endl;
cout << "1. Sign in" << endl;
cout << "2. Balance" << endl;
cout << "3. Deposit" << endl;
cout << "4. Withdraw" << endl;
cout << "5. Exit" << endl;
cout << "Enter your selection: ";
cin >> ch;
switch (ch)
{
case 1:
a[d].printId();
break;
case 2:
a[d].bal();
break;
case 3:
a[d].dep();
break;
case 4:
a[d].withdraw();
break;
case 5:
cout << "Thank You! Good Bye!";
break;
default:
cout << "Invalid choice" << endl;
break;
}
while (!(d >= 0 && d <= 9))
{
cout << "Enter an id: ";
cin >> d;
if (!(d >= 0 && d <= 9))
cout << "Invalid id! Try again" << endl;
}
}
d = 10;
ch = 0;
}
return 0;
}
Explanation / Answer
updated. Let me know if you have any questions via comment.
#include<iostream>
#include<string>
using namespace std;
class Account
{
private://store balance and id of account
int id;
double balance;
public:
Account()
{
id = 0;
balance = 1000;
}
Account(int a)
{
id = a;
balance = 1000;
}
void printId()
{
cout << "Signed in account: " << id << endl;
}
void withdraw()//define dunction to decrease the ammount in account
{
int draw;
for(int i = 0; i < 2; i++)
{
cout << "Enter the amount to withdraw: ";
cin >> draw;
if (balance < draw)
{
cout << "Insufficient funds. Balance is " << balance << ". Try with different amount!" << endl;
continue;
}
balance = balance - draw;
cout << endl;
return;
}
}
void dep()//define function to deposit the amount into account
{
int dep;
for(int i = 0; i < 2; i++)
{
cout << "Enter the amount to deposit: ";
cin >> dep;
if(dep < 0)
{
cout << "Invalid amount. Try again!" << endl;
continue;
}
balance = balance + dep;
cout << endl;
return;
}
}
void bal()//display the account balance
{
if(balance < 100)
{
cout << "Low balance. ";
return;
}
cout << "The balance is: " << balance << endl;
cout << endl;
}
};
int main()
{
int d, ch;
Account a[10];
for (int i = 0; i < 10; i++)//use for loop to for accounts
{
Account b(i);
a[i] = b;
}
d = 10;
ch = 0;
while (true)
{
while (ch != 5)
{
cout << "Main Menu" << endl;
cout << "1. Sign in" << endl;
cout << "2. Balance" << endl;
cout << "3. Deposit" << endl;
cout << "4. Withdraw" << endl;
cout << "5. Exit" << endl;
cout << "Enter your selection: ";
cin >> ch;
switch (ch)
{
case 1:
a[d].printId();
break;
case 2:
a[d].bal();
break;
case 3:
a[d].dep();
break;
case 4:
a[d].withdraw();
break;
case 5:
cout << "Thank You! Good Bye!";
break;
default:
cout << "Invalid choice" << endl;
break;
}
while (!(d >= 0 && d <= 9))
{
cout << "Enter an id: ";
cin >> d;
if (!(d >= 0 && d <= 9))
cout << "Invalid id! Try again" << endl;
}
}
d = 10;
ch = 0;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.