C++ program Not sure what I am doing wrong, need a fresh perspective please. #in
ID: 3832707 • Letter: C
Question
C++ program
Not sure what I am doing wrong, need a fresh perspective please.
#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 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;
while (true)
{
while (!(d >= 0 && d <= 9))
{
cout << "Enter an id: ";
cin >> d;
if (!(d >= 0 && d <= 9))
cout << "Invalid id! Try again" << endl;
}
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].id();
break;
case 2:
a[d].bal();
break;
case 3:
a[d].dep();
break;
case 4:
a[d].draw();
break;
case 5:
cout << "Thank You! Good Bye!"
break;
default;
cout << "Invalid choice" << endl;
break;
}
}
d = 10;
ch = 0;
}
return 0;
}
Explanation / Answer
#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 (!(d >= 0 && d <= 9))
{
cout << "Enter an id: ";
cin >> d;
if (!(d >= 0 && d <= 9))
cout << "Invalid id! Try again" << endl;
}
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;
}
}
d = 10;
ch = 0;
}
return 0;
}
Fixed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.