Cases and Switch Issue - c++

I am writing a program that simulates an ATM. So it tracks account balances, withdrawals and deposits in a very basic matter.
Everything works well during the first iteration, but if I go to make two or more deposits or withdrawals, the account balances default back to the original amount.
Here is an example of what is currently happening: I have $1,000 in my account initially. I make a deposit of $50. It prints out that I now have $1,050 in my account and asks if I would like to perform any other actions. (This is all good). If I select that I want to make another deposit of $100, it says my new account balance is $1,100 instead of $1,150. It does not store my latest account balance when I perform new withdrawals or deposits.
The second (less important) issue is that each time a withdrawal is made, there is a $2.50 fee for each withdrawal that also gets subtracted from my account balance.
I have not learned loops yet, only Cases, If statements and If Else Statements.
Is it possible to do what I want to do? Below is my code. Thank you in advance! This is my first time posting, so if I have pasted my code wrong, I apologize.
#include <iostream>
#include <string>
using namespace std; // opens library for "cout"
int main()
{
float test;
int logout;
string name;
float balance;
float fee;
int choice;
float withdraw;
float deposit;
float bonus;
bonus = 2.50;
balance = 1572.36;
fee = 12.50;
char answer;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << balance - fee << " \n";
cout << "What would you like to do today?\n";
do
{
cout << "\n1 - Current Balance\n2 - Withdraw\n3 - deposit\n4 - Log "
"Out\nOption: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nCurrent Balance is " << balance - fee - withdraw + deposit
<< " \n";
cout << "Would you like to take any other actions today?\n";
break;
case 2:
cout << "\nWithdraw - How much would you like to withdraw? $";
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< balance - fee - withdraw + deposit << "\n";
cout << "Would you like to take any other actions today?\n";
break;
case 3:
cout << "\nDeposit - How much would you like to deposit? $";
cin >> deposit;
test = balance - fee - withdraw + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl; //<<balance - fee - withdraw + deposit<<"\n";
cout << "Would you like to take any other actions today? Y or N \n";
cin >> answer;
cout << answer;
if (answer == 'y' || 'Y')
{
test = balance - fee - withdraw + deposit + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl;
}
// cout <<"Your new balance after depositing $"<<deposit<<" will be $"
// <<test<< endl; //<<balance - fee - withdraw + deposit<<"\n";
// cout <<"Would you like to take any other actions today?\n";
break;
case 4:
cout << "\nLog Out - Thank you for banking with Pallet Town Bank. Have "
"a great day!";
}
} while (choice != 4);
}

The main issue, as Alan Birtles points out, is that you never update the balance value; you just store the temporary calculations in the test variable.
Here's how you could change your code. I tried interpreting the expected behaviour of the program as best I could from the text:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const double fee = 12.50;
double balance = 1572.36;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
string name;
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << (balance -= fee) << " \n";
cout << "What would you like to do today?\n\n";
while (true)
{
cout << "1 - Current Balance" << '\n'
<< "2 - Withdraw" << '\n'
<< "3 - deposit" << '\n'
<< "4 - Log Out" << '\n'
<< "Option: ";
int choice;
cin >> choice;
cout << endl;
if (choice == 4) break;
switch (choice)
{
case 1:
cout << "Current Balance is " << balance << '\n';
break;
case 2:
cout << "Withdraw - How much would you like to withdraw? $";
double withdraw;
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< (balance -= withdraw) << '\n';
break;
case 3:
cout << "Deposit - How much would you like to deposit? $";
double deposit;
cin >> deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< (balance += deposit) << '\n';
break;
}
cout << "Would you like to take any other actions today? ";
char answer;
cin >> answer;
cout << endl;
if (toupper(answer) == 'N') break;
}
cout << "Log Out - Thank you for banking with Pallet Town Bank. Have a great day!" << endl;
}
Live demo
Changes
First of all, every time a modification is made (withdrawal/deposit), we need to actually update the balance. We can do so by using the += or -= operators (called "compound assignment" operators): when writing balance += x, we're adding x to balance, and when writing balance -= x we're subtracting. These expressions are equivalent to balance = balance + x and balance = balance - x, respectively.
I moved the "Would you like to [...]" part outside the switch statement, to avoid repeating it in each case.
As pointed out in the comments, answer == 'Y' || 'y' isn't the same as answer == 'Y' || answer == 'y'. I changed that to toupper(answer) == 'Y'.
I moved the logout handling outside the while loop, so that whenever the loop terminates, the logout message is always shown. That allows us to remove case 4 from the switch statement, by checking at the beginning if choice == 4 and then breaking out of the loop accordingly. This also implies the loop becomes a while (true) loop. Probably there is a more elegant way.
Even better
If you're confortable with functions, I would suggest refactoring the code, isolating each operation individually:
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
const int FEE = 1250; // fee in cents
//---- Utilities ----//
string moneyString(int cents) {
ostringstream oss;
oss << cents/100 << '.' << cents % 100;
return oss.str();
}
int toCents(double money) {
return int(round(money*100));
}
int getMoney() {
double money;
cin >> money;
return toCents(money);
}
//---- User input ----//
// Available choices
enum Choices {
BALANCE = 1,
WITHDRAW = 2,
DEPOSIT = 3,
LOGOUT = 4
};
short int getChoice() {
short int choice = 0;
while (choice < 1 or choice > 4) {
cout << "1 - Current Balance" << '\n'
<< "2 - Withdraw" << '\n'
<< "3 - deposit" << '\n'
<< "4 - Log Out" << '\n'
<< "Option: ";
string input;
cin >> input;
choice = atoi(input.c_str());
cout << endl;
}
return choice;
}
bool userWantsMoreActions() {
cout << "Would you like to take any other actions today? ";
char answer;
cin >> answer;
cout << endl;
return toupper(answer) == 'Y';
}
//---- Actions ----//
void greeting(double &balance) {
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
string name;
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << moneyString(balance) << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your account.\n";
cout << "Your updated balance will be $" << moneyString(balance -= FEE) << " \n";
cout << "What would you like to do today?\n\n";
}
void printBalance(const double &balance) {
cout << "Current Balance is " << balance << '\n';
}
void withdraw(double &balance) {
cout << "Withdraw - How much would you like to withdraw? $";
int withdraw = getMoney();
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< (balance -= withdraw -= FEE) << '\n';
}
void deposit(double &balance) {
cout << "Deposit - How much would you like to deposit? $";
int deposit = getMoney();
cout << "Your new balance after depositing $" << moneyString(deposit)
<< " will be $" << moneyString(balance += deposit -= FEE) << '\n';
}
int main()
{
// Initialize a sample session:
double balance = 157236;
greeting(balance);
while (true)
{
short int choice = getChoice();
if (choice == Choices::BALANCE) printBalance(balance);
else if (choice == Choices::WITHDRAW) withdraw(balance);
else if (choice == Choices::DEPOSIT) deposit(balance);
else if (choice == Choices::LOGOUT) break;
if (not userWantsMoreActions()) break;
}
cout << "Log Out - Thank you for banking with Pallet Town Bank. Have a great day!" << endl;
}
Live demo

Related

how to add all in all the total in loop?

It is me again. I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed.
int main()
{
int choice;
int purchase;
int quantity;
double totalChoice1;
double totalChoice2;
char view;
char confirm;
char buyMore;
char look;
double alloy, apex, kraken, aorus;
double oppo, alpha, rog, huawei;
double ps4, nintendo, xbox, wii;
alloy = 69.99;
apex = 199;
kraken = 90;
aorus = 60;
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "--------------------" << endl;
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
cout << "[2] SteelSeries APEX PRO - $199" << endl;
cout << "[3] Razer Kraken X - $90" << endl;
cout << "[4] AORUS K7 - $60" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> purchase;
cout << "--------------------" << endl;
if (purchase == 1) {
cout << "How many would you like to purchase? ";
cin >> quantity;
totalChoice1 = quantity * alloy;
cout << "The total price for that is " << totalChoice1 << endl;
cout << "Confirm the Purchase? [Y]/[N]: ";
cin >> confirm;
if (confirm == 'Y') {
totalChoice1; // This is just a trial code.
cout << "Would you like to buy more items? [Y]/[N]: ";
cin >> buyMore;
}
else if (confirm == 'N') {
cout << "Do you still want to look for items? [Y]/[N]: ";
cin >> look;
if (look == 'N') {
break;
}
}
else {
break;
}
}
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
You are simply missing a variable to keep track of that total. Don't forget to give it an initial value of 0!
There are plenty of minor other issues with your code, so you'll have some more learning ahead. For instance, we find it easier to do bookkeeping in cents, because you can treat them as integers.

How can I write a better and cleaner version of my bank account code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have this small bank account code to make an account and to access an already made account. It is pretty procedural but I need help knowing what to do to make it better. Like I want an option to loop back after deposit is made and withdraw is made and a separate key to exit the console application.
int main(){
//MIKE BANK LTD
string name;
string defacctNum = "123456";
string acctNum;
int defacctPin = 1357;
int acctPin;
double acctBal;
double defacctBal = 100.59;
int withdraw;
int deposit;
int check;
int yo;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|Hello customer, welcome to Obadan Bank. Do you already have an account with us?|" << endl;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|Enter 1 if you have an account or 2 if you want to create a new one.|" << endl;
cin >> check;
if (check == 1) {
cout << "Enter account number: ";
cin >> acctNum;
while (acctNum != defacctNum) {
cout << "Wrong account number not recognized try again: ";
cin >> acctNum;
}
if (acctNum == defacctNum) {
cout << "Enter your pin: ";
cin >> acctPin;
while (acctPin != defacctPin) {
cout << "Wrong pin please enter it again: ";
cin >> acctPin;
}
if (acctPin == defacctPin) {
cout << "You have $" << defacctBal << " in you account." << endl;
int check2;
cout << "Would you like to deposit or withdraw? Press 1 to deposit, 2 to withdraw or any other key to exit." << endl;
cin >> check2;
if (check2 == 1) {
cout << "Enter the amount you want to deposit.: " << endl;
cin >> deposit;
cout << "You deposited $" << deposit << ".";
defacctBal += deposit;
cout << "Your account balance is now $" << defacctBal << "." << endl;
}
else if (check2 == 2) {
cout << "Enter amount you want to withdraw." << endl;
cin >> withdraw;
while (withdraw > defacctBal) {
cout << "You can't withdraw more than you have!" << endl;
cin >> withdraw;
}
if (withdraw < defacctBal) {
defacctBal -= withdraw;
cout << "You withdrew $" << withdraw << ", now you have $" << defacctBal << endl;
}
}
}
}
}
else if (check == 2) {
int acctNums;
cout << "Enter your name: ";
cin >> name;
cout << "Welcome to Obadan Bank, " << name << ", we would be generating an account number for you.";
acctNums = rand() % 999999 + 100000;
cout << "..l o a d i n g..." << endl;
cout << "You new account number is: " << acctNums << ". Please enter your new pin: " << endl;
cin >> acctPin;
cout << "Confirm pin again." << endl;
int pinConf;
cin >> pinConf;
while (acctPin != pinConf) {
cout << "Please make sure both pins match!" << endl;
cin >> pinConf;
}
if (pinConf == acctPin) {
cout << "Welcome to your new account, " << name << ". Would you like to start off with a deposit? Hit 1 to deposit or any other key to exit." << endl;
int conf;
cin >> conf;
if (conf == 1) {
cout << "Enter your deposit amount." << endl;
cin >> deposit;
cout << "Great! You deposited $" << deposit << "." << endl;
}
}
}
cin >> yo;
return 0;
}
Although as suggested by Paul, Questions about improving working code belong on codereview.stackexchange.com, but still here's a short architectural answer to your question
1)Create a BankAccount Class along with a BankCustomer class something like this:
class BankCustomer
{
//Member variables representing state of the object
BankAccount bankAcct;
std::string customerName;
.... //All other customer specific details in form of member variables
//Member functions for performing operations on this object
}
class BankAccount
{
//Member variables representing "state"
//Member functions to perform operations like:
"CreateAccount()"
"DepositToExistingAccount(int accountNumber)"
"WithdrawFromExistingAccount(int accountNumber)"
};
In your client application(say main.cpp), create BankCustomer objects in a do-while loop. Imagine that this is the bank manager performing this operation to service different BankCustomers.
int main()
{
std::string option;
cin>>option;
do
{
//Here ask the different choices like
1. New User Creation
2. Operations on Existing User:
a) Deposit
b) Withdraw
3. Exit
}while(option != "Exit")
}
Cheers,
Deepak

Simple c++ ATM (can't store balance variable input)

I'm making a simple c++ atm program, but I'm having trouble with getting the balance to change after I make a deposit or withdraw.
// C++ ATM
#include "std_lib_facilities.h"
int main()
{
bool card_is_inserted = false;
double balance = 0.0;
//double new_balance = balance;
// HOME
//Starts over if variable is false
while (card_is_inserted == false)
{
cout << "Wellcome to Well's Fargo ATM " << '\n'
<< "Insert card Yes or No"<< endl;
string request;
getline(cin,request);
// Function is needed for aceppting different no's and yes's
//-=-=-=--=-==--=-=-==-==-=--==--=-=-
// loads atm
if (request == "yes")
{
cout << "Alright, Your current balance is:" << endl
<< balance << endl;
card_is_inserted = true;
}
// home
string option = "cancel";
while (card_is_inserted == true)
{
cout << "Would you like to withdraw or deposit? (Cancel)"<< endl;
getline(cin,option);
double cash_ = 0;
if (option == "deposit")
{
cout << "How much money would you like to deposit?" << endl;
cin >> cash_;
double new_deposit_balance = balance + cash_;
cout << "You placed: $" << cash_ << endl
<< "Your New Balance is: $" << new_deposit_balance << endl;
}
if (option == "withdraw")
{
cout << "How much money would you like to withdraw?" << endl;
cin >> cash_;
double new_witdraw_balance = balance - cash_;
if(balance <= 0)
{
cout << "You don't have: $" << cash_ << endl;
}
else
{
cout << "You toke: $" << cash_ << endl
<< "Your New Balance is: $"<< new_witdraw_balance << endl;
}
}
if (option == "cancel")
{
cout << "Ok, bye" << endl;
card_is_inserted = false;
}
}
}
}
example: I type yes to make a deposit(or withdraw) and then place a simple double like 12.50 then it shows me my current balance which will be 12.50; afterward I want to make a withdraw of 12.00 with .50 left. But I cant because the balance variable didn't store my previous value which was 12.50. I tried making "double new_balance = balance" but doesn't work like in swift.
You are not setting balance to new_witdraw_balance or new_deposit_balance.
double new_deposit_balance = balance + cash_; doesn't set the balance value because you are bring in the value of balance, but you are not assigning the outcome of balance + cash_ to balance.
You need to put something like balance = new_witdraw_balance; and balance = new_deposit_balance; at the end of each if after cout statement.
if(option == "deposit")
{
cout << "How much money would you like to deposit?" << endl;
cin >> cash_;
double new_deposit_balance = balance + cash_;
cout << "You placed: $" << cash_ << endl << "Your New Balance is: $" << new_deposit_balance << endl;
balance = new_deposit_balance; // this
}
if(option == "withdraw")
{
cout << "How much money would you like to withdraw?" << endl;
cin >> cash_;
double new_witdraw_balance = balance - cash_;
if(balance <= 0)
{
cout << "You don't have: $" << cash_ << endl;
}
else
{
cout << "You toke: $" << cash_ << endl << "Your New Balance is: $"<< new_witdraw_balance << endl;
balance = new_witdraw_balance; // and this
}
}
The line double new_deposit_balance = balance + cash_; only assigns the new balance to new_deposit_balance, but then you don't do anything with that variable (except print the value). If you want the new balance to persist, you actually need to modify balance, not new_deposit_balance, so something like balance = balance + cash_; or balance += cash_;.
The variable double new_deposit_balance only exists in the if-block it's defined in, so once you leave the if-block, you lose the information in new_deposit_balance. On the other hand, since balance is defined outside your if-blocks and while-loops, its value will persist throughout your ATM operations.
Of course, you'd also need to apply the same fix to new_witdraw_balance.

error expected expression on do while loop c++

I am not done with my code yet but so far I have a error, all the way in the bottom where I am trying to do my do while statement the first line
cout << "Please enter one of the following: \n"; says expected expression so it is not letting me run the code, help please?
#include <iostream>
using namespace std;
int main() {
int numTickets, options, count = 0;
double total, discount, costTicket = 10, discountPrice = .05;
char member;
cout << ":)\t Welcome to the Regal Entertainment Group Theater\t :)\n"
<< ":)\t ****The greatest movie theater in the world.****\t :)\n\n";
cout << "Please enter one of the following: \n";
cout << "1 - I want tickets to watch the movie!\n"
<< "2 - I'm out.\n";
cin >> options;
cout << "How many tickets will you need to watch your movie tonight? ";
cin >> numTickets;
cout << "Are you a member of our marvelous Regal Crown Club (Y/N)? ";
cin >> member;
if(numTickets <= 0)
cout << "Please enter a number greater than 0.";
else if(numTickets < 4)
costTicket = 10;
else if(numTickets >= 5)
costTicket = 8;
if(member == 'Y' || member == 'y')
{
discount = costTicket * numTickets * discountPrice;
total = numTickets * costTicket - discount;
cout << "Your total for the movie tickets is going to be: ";
cout << "$" << total << endl;
cout << "You saved $" << discount << endl;
cout << "You can pick up your free small popcorn at the stand.\n";
cout << "Thanks for coming to watch your movie at Regal Entertainment Group!\n";
}
else
{
total = numTickets * costTicket;
cout << "Your total for the movie tickets is going to be: ";
cout << "$" << total << endl;
cout << "Thanks for coming to watch your movie at Regal Entertainment Group!\n";
}
system("cls");
do
{
cout << "Please enter one of the following: \n";
cout << "1 - I want tickets to watch the movie!\n";
cout << "2 - I'm out.\n";
cin >> options;
}while(options != 2);
return 0;
}

C++ Basic Menu Driven Program

I have a question that should be simple but I can't find the answer anywhere.
I have a menu driven C++ program that works perfectly when the menu options are numbers but I can't figure out how to change the numbers to letters.
For example:
works fine when choices are 1, 2, or 3 but not A, B, C.
Not sure how I am supposed to declare the option... is it a char? thanks in advance
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Variables needed for the problem
int numbServCharge; //number of service charges
char choice; //menu choice
double transAmt, balence, totalServCharge; //transaction amount, current balence, total service charges
//Constant service change
const double servCharge = 0.25; //constant $0.25 for service charges
numbServCharge = 0; //Start the counter for number of service charges at zero
cout << "Checkbook Balencing Program\n\n";
cout << "Enter the beginning balence: ";
cin >> balence; //get the initial balence
cout << endl;
do
{
//Highlight menu options
cout << "\nCommands\n";
cout << "C - process a check\n";
cout << "D - process a deposit\n";
cout << "E - end the program\n\n";
//Get user's choice from menu
cout << "Enter transaction type: ";
cin >> choice;
cout << endl;
//Create an error message for invalid choice and get a second choice
while((choice != 'C') && (choice != 'D') && (choice != 'E')
{
cout << "Invalid selection. Please choose C, D or E: ";
cin >> choice;
}
//Set up for option #1 -- using a check
if(choice=='C')
{
cout << "Enter transaction amount: ";
cin >> transAmt; //Get the amount of the check
cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
transAmt = transAmt + servCharge; //Add the service charge onto the transaction
numbServCharge++; //Add one to the number of service charges there have been
balence = balence - transAmt; //Update account balence
cout << "\nBalence: $" << fixed << setprecision(2) << balence;
cout << "\nService charge: $0.25 for a check\n";
totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
}
//Set up for option #2 -- deposits
if(choice=='D')
{
cout << "Enter transaction amout: ";
cin >> transAmt; //Get the amount of the deposit
cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
transAmt = transAmt - servCharge; //Add the service charge onto the deposit
numbServCharge++; //Add one to the number of service charges there have been
balence = balence + transAmt; //Update account balence
cout << "Balence: $" << fixed << setprecision(2) << balence << endl;
totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
}
}while(choice != 'E'); //Close program if option 3 is selected
//Display final balence
cout << "Processing end of the month";
cout << "\nFinal balence : $ " << fixed << setprecision(2) << balence << endl << endl;
system("pause"); //Pause screen so it doesn't close right away
return 0;
}
When testing for a string, you should convert everything to a common case. In you case you should convert the user input to upper case. You can do this by using toupper function
Here is the bit of code you need to change to make your program work
cout << "Enter transaction type: ";
cin >> choice;
choice = toupper(choice); // Change Here
cout << endl;
Once you change while((choice != 'C') && (choice != 'D') && (choice != 'E') to while((choice != 'C') && (choice != 'D') && (choice != 'E')), your code runs well. Although I would have personally used a std::string instead of a char.
Granted, Caesar's answer is a valid point - and I would change that as well. However, it would be better as a comment because it doesn't resolve the problem. Wait, hold on. What problem? Your program seems to still "run perfectly" even with the alphabetic menu options.
The only bug in your program is when you try and assign balence (or any of your three doubles) to a non-numeric entry. When I type "C" for initial balance, I see this monstrosity:
Over and over and over. That's not fun. Similar thing happens if I type a letter for transaction amount:
Solution: Never try to jam input taken directly from the user into a variable that is not a type of string. Use a built in string-to-number function like atof, or (much preferred) add error handlers like this:
if (std::cin >> dbl)
{
//input is a double. handle that here.
}
else
{
//input isn't a double. handle that here.
}
By the way, it's spelled balance, not balence ;)