Feb 17, 2017 at 11:33pm UTC
Im not finished with my code yet but Im just trying to figure out how I can make my code say "Error. Please enter one of the available options provided." when the user inputs a Letter?
Please give me an easy explanation.
// Kenny Maldonado
// Lab 1: Bank Account Class
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class bankAccount
{
private:
double balance = 0;
double rate = 0;
public:
double getBalance();
double getRate();
void deposit(double amount);
void withdrawal(double amount);
void accrueInterest(int yearlyPeriod);
};
double bankAccount::getBalance()
{
return balance;
}
double bankAccount::getRate()
{
return rate;
}
void bankAccount::deposit(double amount)
{
if (amount > 0 && amount <= 10000)
balance = amount + balance;
else
{
cin.clear();
cout << "Error. Please enter a value from 1-10000." << endl;
}
}
void bankAccount::withdrawal(double amount)
{
if (amount > balance)
{
cin.clear();
cout << "Error. You cannot withdraw $" << amount << " since balance is $ " << balance << endl;
}
else
balance = balance - amount;
}
void bankAccount::accrueInterest(int yearlyPeriod)
{
double rate = 0.00;
if (balance < 1)
{
cout << "Insifficient Funds. Please make a deposit to calculate your Interest" << endl << endl;
return;
}
while (yearlyPeriod < 1 || yearlyPeriod > 24)
{
cout << "You entered a Yearly Period that does not range between 1 and 24 " << endl << endl;
cout << "Please enter a value from 1 to 24: " << endl << endl;
cin >> yearlyPeriod;
}
cout << " Enter an Interest Rate of your choice" << endl << endl;
cout << "Our Maximum Interest Percentage is 0.05 %" << endl << endl;
cin >> rate;
{
while (rate < 0.01 || rate > 0.05)
{
cin.clear();
cout << "Error. Please enter a Rate of your choice: " << endl << endl;
cout << "Our Maximum Interest Percentage is 0.05 %" << endl << endl;
cin >> rate;
}
}
rate = rate;
cout << "With the entered Balance " << balance << ", the interest accrued after " << yearlyPeriod << " years will total:\n\n $";
cout << balance*(rate)*(yearlyPeriod);
cout << " dollars." << endl << endl;
}
int depositAmount;
int withdrawal;
int numOfChoice;
int balance;
double rate;
double c;
int main()
{
bankAccount act1;
cin.clear();
cout << "Hello, Welcome to Intel Banking!" << endl<< endl;
cout << "What would you like to do today?" << endl<< endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
while (numOfChoice < 1 || numOfChoice >6)
{
cout << "Error. Please enter one of the available options provided." << endl << endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
}
do
{
switch (numOfChoice)
{
case 1:
cout << "Please deposit an amount from 1-10000$?" << endl << endl;
cin >> depositAmount;
act1.deposit(depositAmount);
cout << "You have deposited: $" << depositAmount << endl << endl;
cout << "What else would you like to do?" << endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
break;
case 2:
cout << "How much will you be withdrawing?" << endl;
cin >> withdrawal;
act1.withdrawal(withdrawal);
cout << "You withdrew: $" << withdrawal << endl << endl;
cout << "What else would you like to do?" << endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
break;
case 3:
cout << "Your Balance is: $" << act1.getBalance() << endl;
cout << "What else would you like to do?" << endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
break;
case 4:
cout << "Current Interest Rate: " << act1.getRate() << endl << endl;
cout << "What else would you like to do?" << endl << endl;
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
break;
case 5:
cout << "Input the amount of Years betweeen (1-24)" << endl << endl;
cin >> c;
act1.accrueInterest(c);
cout << "1) Make deposit" << endl << endl;
cout << "2) Withdraw Money" << endl << endl;
cout << "3) Balance" << endl << endl;
cout << "4) Appointed Interest" << endl << endl;
cout << "5) Interest Rate Calculator" << endl << endl;
cout << "6) Exit" << endl << endl;
cin >> numOfChoice;
break;
case 6:
if (numOfChoice = 6)
cout << "Thank you for Banking with Intel" << endl << endl;
cin >> numOfChoice;
break;
}
} while (numOfChoice = numOfChoice);
system("pause");
return 0;
}
Feb 17, 2017 at 11:48pm UTC
you can cin a character, and then add a default to the switch statement if it isn't accepted.
char numOfChoice; instead of int and
default:
cout << "Invalid input and stuff";
Feb 18, 2017 at 12:31am UTC
When I do that it wont let me input the actual numbers that im suppose to input? For example, it gives me the error (when i input 1) ?
Feb 18, 2017 at 2:11am UTC
check for '1' instead of 1
I should have said that, sorry.
this only works up to 10, by the way. There are other ways to do it. You could read a string, convert it to integer and trap any errors there, then feed it into the switch.
I thought I was giving the easiest fix, though.
Last edited on Feb 18, 2017 at 2:13am UTC