#include <string>
#include <iostream>
#include <iomanip>
usingnamespace std;
class Acct
{
private:
double balance;
staticconstint transactions = 0;
public:
staticconstint numberCreated = 0;
Acct();
Acct(double initialAmount);
void Deposit(double amount);
double Withdraw(double amount);
double GetBalance();
staticint GetTransactionCount();
};
Acct::Acct()
{
balance = 0;
numberCreated++;//Error C2105: '++' needs l-value
}
Acct::Acct(double initialAmount)
{
balance = initialAmount;
numberCreated++;//Error C2105: '++' needs l-value
}
void Acct::Deposit(double amount)
{
balance += amount;
transactions++;//Error C2105: '++' needs l-value
}
double Acct::Withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
transactions++;//Error C2105: '++' needs l-value
return balance;
}
elsereturn -1.0;
}
double Acct::GetBalance()
{
transactions++;//Error C2105: '++' needs l-value
return balance;
}
int Acct::GetTransactionCount()
{
return transactions;
};
staticvoid main()
{
Acct *myAcct = new Acct(25.00);
myAcct->Deposit(700.00);
if (myAcct->Withdraw(300.00) < 0)
std::cout << "Insufficient funds" << std::endl;
if (myAcct->Withdraw(450.00) < 0)
std::cout << "Insufficient funds" << std::endl;
std::cout << "My balance after completing transactions is " << myAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
Acct *yourAcct = new Acct();
yourAcct->Deposit(1234.56);
std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
std::cout << "Number of accounts created is " + Acct::numberCreated << std::endl;
}
I want my output to look like this:
Insufficient funds
My balance after completing transactions is $425.00
The number of transactions is 2
Your balance after completing transactions is $1,234.56
The number of transactions is 4
Number of accounts created is 2
Press any key to continue . . .
okay..how do I create a counter for the number of transactions and accounts created then without static constant integers because i got this code to work when it was written in C# initially this way but trying to convert it over does not work.
Drop the const keyword so you can increment them. The problem, then, is that you cannot initialise static members in place like that. Initialize static members outside of the class (in the top of the source file if you are using separate header and source files).
1 2 3 4 5 6 7
class Acct
{
staticint numberCreated;
//...
};
int Acct::numberCreated = 0;
1) Remove the 'const' keyword. That makes it constant (ie: you can't modify it)
2) Remove the = 0 from the class body. You can't initiallize non-const members in the class body like that
3) Instantiate the transactions variable by putting it outside the class
Here's what the code should look like:
1 2 3 4 5 6 7 8 9 10
class Acct
{
//...
staticint transactions; // note, not 'const'
//...
};
// then, globally, in ONE AND ONLY ONE cpp file:
int Acct::transactions = 0; // instantiate 'transactions' and assign it its initial value
#include <string>
#include <iostream>
#include <iomanip>
usingnamespace std;
class Acct
{
private:
double balance;
staticint transactions;
public:
staticint numberCreated;
Acct();
Acct(double initialAmount);
void Deposit(double amount);
double Withdraw(double amount);
double GetBalance();
staticint GetTransactionCount();
};
int Acct::numberCreated = 0;
int Acct::transactions = 0;
Acct::Acct()
{
balance = 0;
numberCreated++;
}
Acct::Acct(double initialAmount)
{
balance = initialAmount;
numberCreated++;
}
void Acct::Deposit(double amount)
{
balance += amount;
transactions++;
}
double Acct::Withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
transactions++;
return balance;
}
elsereturn -1.0;
}
double Acct::GetBalance()
{
transactions++;
return balance;
}
int Acct::GetTransactionCount()
{
return transactions;
};
staticvoid main()
{
Acct *myAcct = new Acct(25.00);
myAcct->Deposit(700.00);
if (myAcct->Withdraw(300.00) < 0)
std::cout << "Insufficient funds" << std::endl;
if (myAcct->Withdraw(450.00) < 0)
std::cout << "Insufficient funds" << std::endl;
std::cout << "My balance after completing transactions is " << myAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
Acct *yourAcct = new Acct();
yourAcct->Deposit(1234.56);
std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
std::cout << "Number of accounts created is " << Acct::numberCreated << std::endl;
}
Output
Insufficient funds
My balance after completing transaction 425
The number of transactions is 3
Your balance after completing transaction 1234.56
The number of transactions is 5
Number of accounts created is 2
Press any key to continue . . .
One more question why does the code increment one more than my initial C# code? I know I can set the initial value to negative one like so: int Acct::transactions = -1; if I want to get desired result..just wondering.Thanks again all....
One more question why does the code increment one more than my initial C# code? I know I can set the initial value to negative one if I want if I want the desired result..just wondering.Thanks again all....
Are you sure you want line 66 to count getting the balance as a transaction?
Yea I think your right but when I took out line 66 the output is:
Insufficient funds
My balance after completing transactions is 425
The number of transactions is 2
Your balance after completing transactions is 1234.56
The number of transactions is 3
Number of accounts created is 2
Press any key to continue . . .
What should I do? There are suppose to be 4 transactions all together 2 deposits 2 withdrawals.
Acct *myAcct = new Acct(25.00);
myAcct->Deposit(700.00);
if (myAcct->Withdraw(300.00) < 0)
std::cout << "Insufficient funds" << std::endl;
if (myAcct->Withdraw(450.00) < 0)
std::cout << "Insufficient funds" << std::endl;
std::cout << "My balance after completing transactions is " << myAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
Acct *yourAcct = new Acct();
yourAcct->Deposit(1234.56);
std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
std::cout << "Number of accounts created is " << Acct::numberCreated << std::endl;