Hello everyone, first i apologize for creating multiple threads i will make sure to keep everything on one thread from now on. I an have the program compiling but Im not getting the expected values for my variables. It is supposed to have no pointers just an object array, functions, constructors, and data abstraction. For Example the balance varaible is an outrageous number when its supposed to be between 10,000 and 20,000. If anyone can help me spot the mistake it would be very appreciated. Thanks.
#include <iostream>
usingnamespace std;
class Account
{
private:
int accountId;
double balance;
double annualInterestRate;
public: //contsructors
Account();
Account(int id, double bal, double interest);
public: //setters
void setAccountId(int x);
void setBalance(double x);
void setInterest(double x);
public: // getters
int getAccountId();
double getBalance();
double getInterest();
public:
double getMonthlyInterestRate();
double getMonthlyInterest();
bool withdraw(double amount);
void deposit(double amount);
};
Account::Account()// bodies of functions
{
}
Account::Account(int id, double bal, double interest)
{
}
void Account::setAccountId(int x)
{
accountId = x;
}
void Account::setBalance(double x)
{
balance = x;
}
void Account::setInterest(double x)
{
annualInterestRate = x;
}
int Account::getAccountId()
{
return accountId;
}
double Account::getBalance()
{
return balance;
}
double Account::getInterest()
{
return annualInterestRate;
}
double Account::getMonthlyInterestRate()
{
return annualInterestRate / 12;
}
double Account::getMonthlyInterest()
{
return getMonthlyInterestRate() * balance;
}
bool Account::withdraw(double amount)
{
if (balance >= amount)
returntrue;
elsereturnfalse;
}
void Account::deposit(double amount)
{
setBalance(balance + amount);
}
void menu();
int AccountNumber();
int main()
{
int id = 0;
int amount = 0;
int selection = 0;
Account object;
menu();
while (cin >> selection && selection != -1)
{
switch (selection)
{
case 1:
cout << "Please enter a deposit amount: $ \n";
cin >> amount;
cout << endl;
object.deposit(amount);
break;
case 2:
cout << "Please enter the amount you wish to withdraw: $ \n";
cin >> amount;
cout << endl;
object.withdraw(amount);
break;
case 3:
cout << "The accounts current balance is : $\n"
<< object.getBalance()
<< endl;
break;
case 4:
cout << "Monthly interest rate : \n"
<< object.getMonthlyInterestRate()
<<"%"
<< endl;
cout << "Yearly Interest rate : \n"
<< object.getInterest()
<<"%"
<< endl;
break;
case 5:
cout << "Account ID : \n"
<< id
<< endl;
cout << "Account Balance : $\n"
<< object.getBalance()
<< endl;
cout << "Monthly Interest rate : \n"
<< object.getMonthlyInterestRate()
<< "%"
<< endl;
cout << "Monthly Interest amount earned: $\n"
<< object.getMonthlyInterest()
<< endl;
break;
default:
cout << "Invalid\n";
}
menu();
}
return 0;
}
void menu()
{
cout << "Enter 1 to make a deposit" << endl;
cout << "Enter 2 to make a withraw" << endl;
cout << "Enter 3 to check balance" << endl;
cout << "Enter 4 to check interest rate" << endl;
cout << "Enter 5 to display account summary" << endl;
cout << "Enter -1 to return to main menu" << endl;
}
int AccountNumber()
{
int id;
cout << "Please enter an account number from 0 - 9";
cin >> id;
return id;
}
Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
1
Please enter a deposit amount: $
200
Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
3
The accounts current balance is : $
200
Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
#include <iostream>
#include <string>
#include <iomanip>
#include "Account.h"
void menu();
int main()
{
constint TEST_ACCOUNTS = 10;
// Declaring an array of Account(s).
Account accounts[TEST_ACCOUNTS];
// Let's look inside them:
for(int i = 0; i< TEST_ACCOUNTS; i++) {
std::cout << "\nAccount n. " << i
<< "\naccountId = " << accounts[i].getAccountId()
<< "; balance = " << accounts[i].getBalance()
<< "; interest rate = " << accounts[i].getInterest()
<< '\n';
}
// Ok, it looks we have all date in the ranges we decided.
// BTW, it happened because the Account::Account() constructor
// has been invoked for every account.
// The first problem is: more than one account could have the
// same accountId, which is not logic.
// Now, let's ask our user what s/he wants:
int selection = 0;
do {
menu();
std::cin >> selection;
int index = 0;
if(selection > 0) {
std::cout << "Please, indicate on which account do you want ""to operate [0-9]: ";
std::cin >> index;
}
double amount = 0.0;
switch (selection)
{
case -1:
std::cout << "Goodbye.\n";
break;
case 1:
std::cout << "Please enter a deposit amount: $ ";
std::cin >> amount;
accounts[index].deposit(amount);
break;
case 2:
std::cout << "Please enter the amount you wish to withdraw: $ ";
std::cin >> amount;
if(accounts[index].withdraw(amount)) {
std::cout << "Ok, you have taken your money back\n";
} else {
std::cout << "Sorry, you don't have such a sum!\n";
}
break;
case 3:
std::cout << "This account current balance is : $"
<< std::fixed << std::setprecision(3)
<< accounts[index].getBalance()
<< std::endl;
break;
case 4:
std:: cout << "Monthly interest rate on this account: "
<< std::fixed << std::setprecision(2)
<< accounts[index].getMonthlyInterestRate()
<< "%; yearly interest rate : "
<< std::fixed << std::setprecision(2)
<< accounts[index].getInterest()
<< "%"
<< std::endl;
break;
case 5:
std::cout << "Account ID : " << accounts[index].getAccountId()
<< "\nAccount Balance : $"
<< std::fixed << std::setprecision(2)
<< accounts[index].getBalance()
<< "\nMonthly Interest rate : "
<< accounts[index].getMonthlyInterest()
<< "%; monthly Interest amount earned: $"
<< accounts[index].getMonthlyInterest()
<< std::endl;
break;
default:
std::cout << "Invalid\n";
break;
}
} while (selection != -1);
return 0;
}
void menu()
{
std::cout << "\nEnter 1 to make a deposit\n";
std::cout << "Enter 2 to make a withraw\n";
std::cout << "Enter 3 to check balance\n";
std::cout << "Enter 4 to check interest rate\n";
std::cout << "Enter 5 to display account summary\n";
std::cout << "Enter -1 to return to main menu\n";
}
Please, do not make vague and general questions.
The first step could be to make a simplified version of you code which reproduces the errors you come up against.