So when my code runs it doesn't do the operations I thought I told it to do... did I screw up my if, else if, while statements? Or am I just missing something.... Any help would be great thanx
:)
#include <iostream>
#include <windows.h>
using std::cout;
using std:: cin;
using std:: endl;
usingnamespace std;
class BankAccount
{
public:
BankAccount(void); // Base Class
int getAccountNum(void);
double getAccountBalance(void);// return balance
void depositMoney(double depositAmount);// Deposit money
bool withdrawMoney(double withdrawAmount); // Withdraw money
void setAccountNum(int newAccountNum);// Set up account
int newAccountNum;
double accountBalance;
protected:
int accountNum;
};
//////////////////////////////////////////////////////////////////////
class CheckingAccount :public BankAccount
{
public:
CheckingAccount(void);
bool withdrawMoney(double);
private:
int transactionCount;
};
//////////////////////////////////////////////////////
class SavingsAccount : public BankAccount
{
public:
SavingsAccount(void);
double getAccountBalance(void);
void SetInterestRate(double); // set daily interest rate
private:
double InterestRate; // daily interest
int numberofDays; // Days since last transaction
double earnedInterest;// Interest since last transtion
public:
double getInterestEarned(void); // earned interest
int getNumberofDays(void);
int getAccountBalance(double);
};
//////////////////////////////////////////////////////////////
BankAccount::BankAccount(void)
{
accountNum = 0;
accountBalance = 0.0;
}
void BankAccount::setAccountNum (int newAccountNum)
{
accountNum= newAccountNum;
}
int BankAccount::getAccountNum(void)
{
return accountNum;
}
double BankAccount::getAccountBalance(void)
{
return accountBalance;
}
void BankAccount::depositMoney(double depositAmount)
{
accountBalance= depositAmount + accountBalance;
}
bool BankAccount::withdrawMoney(double withdrawAmount)
{
if
(accountBalance - withdrawAmount < 0)
returnfalse;
else
{
(accountBalance = withdrawAmount- accountBalance);
returntrue;
}
}
//////////////////////////////////////////////////////////////
CheckingAccount ::CheckingAccount (void)
{
accountBalance =100.00;
}
/////////////////////////////////////////////////////////////////////
SavingsAccount :: SavingsAccount (void)
{
accountBalance= 100.00;
}
double SavingsAccount::getAccountBalance()
{
numberofDays = rand()%8;
earnedInterest = accountBalance * (numberofDays * InterestRate/(100*365));
return accountBalance+ earnedInterest;
}
void SavingsAccount::SetInterestRate(double newInterestRate)
{
InterestRate = newInterestRate;
}
double SavingsAccount::getInterestEarned(void)
{
return earnedInterest;
}
int SavingsAccount::getNumberofDays(void)
{
return numberofDays;
}
bool CheckingAccount::withdrawMoney( double withdrawAmount)
{
transactionCount++;
if (transactionCount >3)
{
if
(accountBalance - withdrawAmount - 0.5 < 0)
cout << "insuficent Funds";
returnfalse;
while
(accountBalance = withdrawAmount + 0.5)
cout << "Fifty cent fee included for more than three transactions";
cout << endl;
returntrue;
}
if (accountBalance-withdrawAmount <0)
returnfalse;
else
(accountBalance = accountBalance - withdrawAmount);
returntrue;
}
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
char Choice;
int newAccountNum;
double depositAmount;
double withdrawAmount;
double newInterestRate= 0;
int transactionCount=0;
int randNumber;
randNumber = rand();
srand(GetTickCount());
BankAccount accountBank;
CheckingAccount accountChecking;
SavingsAccount accountSavings;
cout<< "Welcome!"<< "Let's start by creating your Checking account";
cout << "Enter a five digit number to create your checking account number ";
cin >> newAccountNum;
cout << "A new checking account was created.";
cout << endl;
cout << "Your Checking account Balance is $" << accountChecking.getAccountBalance();
cout << endl << endl;
cout<< "Would you like to make a Withdraw or Deposit?"<< "Enter 'W' for withdraw or 'D' for deposit";
cin >> Choice;
do
{
if (Choice == 'W'|| Choice == 'w')
{
cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance is $" << accountChecking.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'S' to set up Savings Account" ;
cin >> Choice;
}
elseif (Choice='D' || Choice == 'd')
{
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is: "; accountChecking.depositMoney( depositAmount);
cout << endl;
cout << "Would you like to make another Transaction";
cout << "Enter 'D' for Deposit and 'W' for Withdrawal or 'S' to create a Savings Account" ;
cin >> Choice;
}
}
while (Choice == 'S' || Choice == 's');
{
cout << "Lets set up an Account Number for your Savings Account!";
cout<< endl;
cout << "Enter a five digit Number for a new savings account : ";
cin >> newAccountNum;
cout << "A savings account has been created";
cout << endl;
cout << "Savings account balance is $" << accountSavings.getAccountBalance();
cout << endl;
cout << "Total interest earned: ";
cout << accountSavings.getInterestEarned();
cout << " over " ;
cout << accountSavings.getNumberofDays();
cout << " days" ;
cout << endl;
cout << "Total interest earned: " << accountSavings.getInterestEarned() << " over" << accountSavings.getNumberofDays() << " days";
cout << endl;
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
if (Choice == 'W'|| Choice == 'w' )
{
cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance is $" << accountSavings.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
elseif (Choice=='D' || Choice == 'd')
{
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is: "; accountSavings.depositMoney( depositAmount);
cout << endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
while (Choice== 'E' || Choice== 'e');
{
cout<< "have a nice day... Good Bye " ;
}
exit(0);
}
I didnt catch the one= and not two, thank you, probably from looking at it so much... lol
Im not quite sure if i know what you mean by line 139 and 140, should it be right after the while, will the code be read different if its not, i dont think i left anything out?
Coder777: I want those if... else if statements to loop(I think) I wanted that when the user had a choice they can pick from any choice. I thought I would use (if, else, else) but visual basic kept giving me an error that I can't use a else statement without a previous if statement... Which I had (Line 244,189). It also told me that my third else it expected a while, so I changed it even though I didn't want to, to get rid of the errors. Ps. Thank you for the tip.
What should I do to get visual basic to let me change that?
Cire: Once again my visual basic was giving me an error telling me that it expected a ';' in that spot so when I put I there the error went away...
... I'm sorry I am not very good at this/troubleshooting my faults...
Don't worry about the VB thing, It distracted me because sometimes people are attempting to translate code from visual basic into c++ (or from c++ into python ...) and that was what I thought you meant at first.
Well, I know the code is fairly long, but it's difficult to give a precise answer without seeing either the latest version of the code, and/or the exact error message as output from the compiler.
It sounds as though you have made some changes in order to keep the compiler happy (which is a good idea), but without understanding exactly why. Perhaps you'd like to post the latest version of the code and state which are the areas giving you concern.
Okay so I went back to how I had the code before I got those compile errors about ("I thought I would use (if, else, else) but visual basic kept giving me an error that I can't use a else statement without a previous if statement... Which I had (Line 244,189). It also told me that my third else it expected a while, so I changed it even though I didn't want to"). I also changed to the constant values first idea... Now I am getting new errors completly. I just thing Visual Studio hates me
#include <iostream>
#incluse <windows.h>
using std::cout;
using std:: cin;
using std:: endl;
usingnamespace std;
class BankAccount
{
public:
BankAccount(void); // Base Class
int getAccountNum(void);
double getAccountBalance(void);// return balance
void depositMoney(double depositAmount);// Deposit money
bool withdrawMoney(double withdrawAmount); // Withdraw money
void setAccountNum(int newAccountNum);// Set up account
int newAccountNum;
double accountBalance;
protected:
int accountNum;
};
//////////////////////////////////////////////////////////////////////
class CheckingAccount :public BankAccount
{
public:
CheckingAccount(void);
bool withdrawMoney(double);
private:
int transactionCount;
};
//////////////////////////////////////////////////////
class SavingsAccount : public BankAccount
{
public:
SavingsAccount(void);
double getAccountBalance(void);
void SetInterestRate(double); // set daily interest rate
private:
double InterestRate; // daily interest
int numberofDays; // Days since last transaction
double earnedInterest;// Interest since last transtion
public:
double getInterestEarned(void); // earned interest
int getNumberofDays(void);
int getAccountBalance(double);
};
//////////////////////////////////////////////////////////////
BankAccount::BankAccount(void)
{
accountNum = 0;
accountBalance = 0.0;
}
void BankAccount::setAccountNum (int newAccountNum)
{
accountNum= newAccountNum;
}
int BankAccount::getAccountNum(void)
{
return accountNum;
}
double BankAccount::getAccountBalance(void)
{
return accountBalance;
}
void BankAccount::depositMoney(double depositAmount)
{
accountBalance= depositAmount + accountBalance;
}
bool BankAccount::withdrawMoney(double withdrawAmount)
{
if
(accountBalance - withdrawAmount < 0)
returnfalse;
else
{
(accountBalance = withdrawAmount- accountBalance);
returntrue;
}
}
//////////////////////////////////////////////////////////////
CheckingAccount ::CheckingAccount (void)
{
accountBalance =100.00;
}
/////////////////////////////////////////////////////////////////////
SavingsAccount :: SavingsAccount (void)
{
accountBalance= 100.00;
}
double SavingsAccount::getAccountBalance()
{
numberofDays = rand()%8;
earnedInterest = accountBalance * (numberofDays * InterestRate/(100*365));
return accountBalance+ earnedInterest;
}
void SavingsAccount::SetInterestRate(double newInterestRate)
{
InterestRate = newInterestRate;
}
double SavingsAccount::getInterestEarned(void)
{
return earnedInterest;
}
int SavingsAccount::getNumberofDays(void)
{
return numberofDays;
}
bool CheckingAccount::withdrawMoney( double withdrawAmount)
{
transactionCount++;
if (transactionCount >3)
{
if
(accountBalance - withdrawAmount - 0.5 < 0)
cout << "insuficent Funds";
returnfalse;
}
else
{
(accountBalance = withdrawAmount + 0.5);
cout << "Fifty cent fee included for more than three transactions";
cout << endl;
returntrue;
}
if (accountBalance-withdrawAmount <0)
returnfalse;
else
{
(accountBalance = accountBalance - withdrawAmount);
returntrue;
}
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
char Choice;
int newAccountNum;
double depositAmount;
double withdrawAmount;
double newInterestRate= 0;
int transactionCount=0;
int randNumber;
randNumber = rand();
srand(GetTickCount());
BankAccount accountBank;
CheckingAccount accountChecking;
SavingsAccount accountSavings;
{
cout<< "Welcome!"<< "Let's start by creating your Checking account";
cout << "Enter a five digit number to create your checking account number ";
cin >> newAccountNum;
cout << "A new checking account was created.";
cout << endl;
cout << "Your Checking account Balance is $" << accountChecking.getAccountBalance();
cout << endl << endl;
cout<< "Would you like to make a Withdraw or Deposit?"<< "Enter 'W' for withdraw or 'D' for deposit";
cin >> Choice;
do
{
if ( 'W'== Choice|| 'w'== Choice)
{
cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance is $" << accountChecking.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'S' to set up Savings Account" ;
cin >> Choice;
}
else
{
(Choice='D' || Choice == 'd');
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is: "; accountChecking.depositMoney( depositAmount);
cout << endl;
cout << "Would you like to make another Transaction";
cout << "Enter 'D' for Deposit and 'W' for Withdrawal or 'S' to create a Savings Account" ;
cin >> Choice;
}
else
{
('S'==Choice || 's'== Choice);
cout << "Lets set up an Account Number for your Savings Account!";
cout<< endl;
cout << "Enter a five digit Number for a new savings account : ";
cin >> newAccountNum;
cout << "A savings account has been created";
cout << endl;
cout << "Savings account balance is $" << accountSavings.getAccountBalance();
cout << endl;
cout << "Total interest earned: ";
cout << accountSavings.getInterestEarned();
cout << " over " ;
cout << accountSavings.getNumberofDays();
cout << " days" ;
cout << endl;
cout << "Total interest earned: " << accountSavings.getInterestEarned() << " over" << accountSavings.getNumberofDays() << " days";
cout << endl;
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
doif ( 'W'==Choice|| 'w'==Choice )
{
cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance is $" << accountSavings.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
else
{
('D'==Choice || 'd'==Choice);
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is: "; accountSavings.depositMoney( depositAmount);
cout << endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
else
{
( 'E'==Choice || 'e'==Choice);
cout<< "have a nice day... Good Bye " ;
}
exit(0);
} }
... Oh and it unformatted it, I'll fix that later though..
Errors:
Line 148: error: a function-definition is not allowed here before '{' token
compilation terminated due to -Wfatal-errors.
Have not seen this before and had the program working with it that exact same way?
You are saying If and then have 3 else statements..
You need to make them 'else if' and save your 'else' for default in the chance that your inputs or conditions are false for every other possible statement
I changed them to else if but I still get this error... Thank you i musat have missed that as well when switching it back to how it was when I got those errors, but it's werid I'm not getting those same errors any more, just this one?
Line 148: error: a function-definition is not allowed here before '{' token
compilation terminated due to -Wfatal-errors.