need help beginning bank atm code, please!

Basically I need to simulate an ATM.
Starting with a balance of 5000.
Then, entering a user-input loop, give the user four choices:
(withdraw, deposit, show balance, quit)

I'm very new to c++ , and it's hard for me to figure how to start, but reading along i do understand. So i'm hoping for a little instruction? I don't need anyone to do the whole thing for me. Maybe just show me how to go about starting it, like after giving the option to press W (to withdraw) , how would i go about withdrawing an amount from the balance. Do i use strings or no? etc

Here's how i started it:
-------------------------------------------------------------------
#include <iostream>
using namespace std;

int main ()
{

int acct_bal = 5000;
int choice;
double withdraw, deposit, balance ;


cout << "Your account balance is $" << acct_bal
<< "\n\nPress (W) if you'd like to withdraw money. \n"
"Press (D) if you'd like to deposit money. \n"
"Press (B) to show balance. \n"
"Press (Q) to exit your account. \n";
cin >> choice;



return 0;

---------------------------------------------------------


Thanks in advance!!
Use and if statement. For example,
1
2
3
if(choice=="D" || choice=="d") {
       ....
}


To do the others use else if.

1
2
3
else if(choice=="B" || choice=="b") {
       ....
}


You may also want to use a do while loop to keep the program running.
1
2
3
4
5
do
{
       //all your code in here
}
while(choice!="Q" || choice!= "q"


So altogether it would look like(after cin >> choice):
1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
       if(choice=="W" || choice=="w") {
              ....//code to withdraw money goes here
       }
       else if(choice=="D" || choice=="d") {
              ....//code to deposit money goes here
       }
       else if(choice=="B" || choice=="b") {
              ....//code to view your balance goes here
       }
}
while(choice!="Q" || choice!="q"); //will quit the program if the user inputs "Q" 
Last edited on
Thanks for the help, do you mind showing how i'd go about setting it up to withdraw the amount they enter?
first, we must declare our variables:
1
2
int balance = 5000; //this is going to act as the variable that stores all the money
int withdraw; //we have no value set for this because the user will need to input it letter 


Now that we have our variables, the code will look a little like this:
1
2
3
4
5
6
if(choice=="W" || choice=="w") {
          cout << "How much money would you like to withdraw?\n";
          cin >> withdraw; //gets the amount of the withdrawal
          balance = balance - withdraw; //sets the balance to the balance minus the amount of money that was withdrawn
         cout << "Your new balance is " << balance; //tells the user their new balance
      }


Now for the deposit do the same except addition and the viewing is just the last line of the withdrawal
The only error coming up is choice is an undeclared identifier..
----------------------------------

#include <iostream>
using namespace std;

int main ()
{
//declare variables
int acct_bal = 5000;
int withdraw;




cout << "Your account balance is $" << acct_bal
<< "\n\nPress (W) if you'd like to withdraw money. \n"
"Press (D) if you'd like to deposit money. \n"
"Press (B) to show balance. \n"
"Press (Q) to exit your account. \n";
cin >> choice;

if (choice == "w" || choice=="W")
{
cout << "enter deposit amount";
cin >> withdraw;
acct_bal = acct_bal - withdraw;
cout << "your new balance is $" << acct_bal;
}



return 0;
}
just use "acctbal"
The only error coming up is choice is an undeclared identifier..

Because you haven't declared it yet. In your // declare variables you should add char choice;

just use "acctbal"

Well he can use whatever he likes for variables names. Just like I could correct you and say use acctBal or even iAccBall or something else. It is up to the programmer's preference as to what naming convention they use. There is no wrong or right way to name things.
oh i thought the problem was the underscore. i looked over the undefined choice
Not to hijack, but underscores perhaps should be avoided -

http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

Note that that kind of thing is beyond me.
No, variable names can have underscores in them. In fact a lot of programmers use them in defines and constants. The only thing you should avoid is starting variables with underscores (_name) and some things are defined with them like __FILE__ or __LINE__.

1
2
3
 
#define DEMON_MACRO 24
const int THIS_IS_A_NUMBER = 30;


So this is fine, just not commonly used:
 
int this_is_a_variable;

Most would have chose a shorter name, but if they used that name they would probably do this:
 
int thisIsNumber;


All are perfectly fine. As far as the compiler is concerned, it is just text (case sensitive mind you).

Basically never start it with an underscore and never start them with a number. I don't think you are allowed to use symbols either in them (never been bored enough to try and they make your code confusing when they are used.
Last edited on by closed account z6A9GNh0
Topic archived. No new replies allowed.