Sign in to follow this  
glyvin

Trying to make a bank program

Recommended Posts

Hi for my Programming class ind study I have to make a program to show loops so I was working on a bank program and I am having problems. I am trying to get it so its a do while loop and if they press 9 it exits here is what I have #include <iostream> #include <stdlib.h> #include <string> using namespace std; int main() { string name; int funds; int oneortwo; int g; int deposit; int currentbalance = 100; cout << "Hello please enter your name to get to your bank information...\n"; cin >> name ; funds: cout << "Thank you " << name << " you have 100 dollars in your bank, would you like to Deposit(1) or Withdrawl(2) funds?" <<endl; cout << "Please select 1 or 2.\n" ; cin >> oneortwo; if (oneortwo =1) { int exit = 8; int dagain; cout << "You have choosen Deposit, how much would you like to deposit? \n" ; cin >> deposit; currentbalance = currentbalance + deposit; cout << "Your balance is now $" <<currentbalance << endl; cout << "Press 1 to make another depsoit or press 9 to quit. " <<"\n"; cin >>dagain; if (dagain =9) exit = exit +1; else while (exit < 10); } Getting an error right at the end I can't figure out were to put the do also, any help would be greatly appreciate, right now if you run it no matter what you type you can't make a 2nd deposit Thanks glyvin

Share this post


Link to post
Share on other sites
This is a homework task so I won't answer directly, but I will give some advice.

Firstly: in future, enclose small snippets of code with [code] and [/code] tags and large snippets with [source] and [/source] tags. It makes it easier for others to read your code and in turn, to help you. :-)

Secondly: as I've said, I feel it's a bad idea to help you directly, but here's a code snippet that's similar in principle to what you're doing (demonstrates the syntax of a do..while loop - which you've currently got wrong in your code - and also shows how to break out.

bool done = false;
do
{
// Do something...

// Set our flag to exit if we can
done = ReadyToExit(); // Where ReadyToExit is a predicate

/* Or we could use the break keyword to break out of the loop:
if (ReadyToExit())
break;
If we do, then we don't need the variable 'done', and the
while testing expression can be changed from '!done' to '1'
or 'true' - an infinite loop.

I think that using the bool 'done' is a better practice however, hence
my choice in this exemplar.
*/


} while(!done);


Now, miscellaneous advice looking through your code.

#include <stdlib.h> should be #include <cstdlib>

I recommend renaming oneortwo to choice. What happens if you add a third menu option? (Okay, in this example it's probably irrelevant - but the idea is to build good practices)

if (dagain = 9) is a problem. Two hints: 1) What does the '=' operator do? 2) If you don't know the answer, try changing it to if (9 = dagain) and see what the compiler says.

exit = exit + 1 can be changed to exit++ or ++exit. However, instead of doing this blindly, I suggest you look up the difference between postincrement and preincrement.

Share this post


Link to post
Share on other sites
A couple of things.

First, use [ source ] tags.

Second, for comparisons (like your "if" statements) you should be using the operator== not "=".

Third, you should list the errors that you are getting.

----

You are missing brackets, it looks like you have a variable named "funds" and a label named "funds", where's the "do". You do not have the deposit input in any loop so it is not surprising that there is no option to make another deposit.

Are you sure that the program runs??? I don't think it should compile. There is no return statement???

Share this post


Link to post
Share on other sites
This is not homework , this is a ind study so I just learn not graded on HW or tests thanks for the reply tho :)

and why not use IF statements?

ok here is what I got and the errors I am recieving


#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;string&gt;
using namespace std;
int main()
{
string name;
int funds;
int oneortwo;
int g;
int deposit;
int currentbalance = 100;
cout &lt;&lt; "Hello please enter your name to get to your bank information...\n";
cin &gt;&gt; name ;
cout &lt;&lt; "Thank you " &lt;&lt; name &lt;&lt; " you have 100 dollars in your bank, would you like to Deposit(1) or Withdrawl(2) funds?" &lt;&lt;endl;
cout &lt;&lt; "Please select 1 or 2.\n" ;
cin &gt;&gt; oneortwo;
if (oneortwo == 1)
{
do
{
int exit = 8;
int dagain;
cout &lt;&lt; "You have choosen Deposit, how much would you like to deposit? \n" ;
cin &gt;&gt; deposit;
currentbalance = currentbalance + deposit;
cout &lt;&lt; "Your balance is now $" &lt;&lt;currentbalance &lt;&lt; endl;
cout &lt;&lt; "Press 1 to make another depsoit or press 9 to quit. " &lt;&lt;"\n";
cin &gt;&gt;dagain;
if (dagain == 9)
exit = exit +1;
else
exit = exit;
}while (exit &lt; 10);



return 0;
}



errors
34 h:\devc__~1\exerci~1\bankpr~1.cpp
ANSI C++ forbids comparison between pointer and integer
40 h:\devc__~1\exerci~1\bankpr~1.cpp
parse error at end of input

Share this post


Link to post
Share on other sites
you need a closing bracket from the "if (oneortwo ==1)" statement.

I wasn't telling you not to use "if" statements, I was trying to show you the correct way to use if statements. Which you fixed. Previously you were assignning new values in between the parenthesis. As long as the new value is not 0 this will always evaluate to true.

You can get rid of the "exit" variable and the last if-else and just change your while(exit<10) to while(dagain!=9).

Or like TheUnbeliever stated,

do
{
//do some stuff
if (dagain==9) done = true;

}while(!done);


HTH

Share this post


Link to post
Share on other sites
Yes, you need an extra closing bracket, that will kill the last error.

The other error is one of scope, the variable 'exit' only exists within the loop and goes out of scope (becomes inacessable) before it can be read by the 'while' statement. You need to declare the variable for the loop condition before it starts.

The reason you're getting this particular compiler error is because there is a function called 'exit' in the STDLIB.H file you included. Since it can't find an 'exit' variable or function in local scope it moves up until it finds one, in this case the 'exit' function in the STD namespace.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
This is a homework task so I won't answer directly, but I will give some advice.


Awesome answer! Most people would stop at 'It's a homework question'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this