-2

I created a bank app, and when I try to deposit money that is over 10,000, my error message that I set up displays, but any number under works.. I assume this has something to do with value types. I used int, however I tried double but can't figure that out too.

/* FUNCTION DEFINITION */
void DepositMoney(int initialbalance, int deposit);

// Main Program
int main()
{
int initialbalance=10000;
int deposit;

cout << " \t ***** What you want to Do ??? Choose the Action character *****" << endl << endl;

            cout <<" \t   W : Money Withdraw" << endl
            <<"\t   D : Deposit" << endl //I CHOSE THIS ONE 
            <<"\t   B : Balance" << endl
            <<"\t   T : Transfer" << endl
            <<"\t   C : Change Details" << endl
            <<"\t   Q : Quit or Exit" << endl << endl ;

            string input;
            cin >> input;
            bool t=true;
                if (input=="W" || input=="w"){

                    cout<<"\n\t Your balance is: " << initialbalance<<endl;
                    cout<<"\n\t How much would you like to withdraw: ";
                    cin>> withdraw;


                   withdrawMoney(initialbalance, withdraw);
                   initialbalance -= withdraw;
                    t=false;

                }
                else if (input=="D" || input=="d"){
                    cout<<"\n\t Your balance is: " << initialbalance<<endl;
                    cout<<"\n\t How much would you like to deposit: ";
                    cin>>deposit;

                    DepositMoney(initialbalance, deposit);
                    initialbalance += deposit;
                    t=false;
// Function for Deposit
    void DepositMoney(int initialbalance, int deposit)
    {
    // Get how much user want to deposit and display balance after adding
                if(initialbalance < deposit)
                        cout <<"\n\t The amount you entered is not not valid."<<endl; //THIS MSG DISPLAYS
                    else
                        initialbalance += deposit;
                    cout<<"\n\t Your new balance is: " << initialbalance <<endl<<endl<<endl;
    }

I tried changing every data type that said int to double, but that didn't even run the code.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 2
    Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Nov 07 '22 at 00:04
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes the full code of a function `main` as well as all `#include` directives. – Andreas Wenzel Nov 07 '22 at 00:06
  • Did you intend to post multiple code blocks? Or did you intend to post all the code as a single code block? – Andreas Wenzel Nov 07 '22 at 00:06
  • `if(initialbalance < deposit)` is the same as `if(deposit > initialbalance)`. So if you try to deposit more money than is currently present, the error message displays. What's this code doing in `DepositMoney` at all, it sounds like a check that only makes sense for withdrawals. – Nathan Pierson Nov 07 '22 at 00:07
  • You might also want to check some of the rules about variable scope and shadowing, or the difference between pass by value and pass by reference. The `initialbalance` variable in the `DepositMoney` function is pretty separate from the `initialbalance` variable in `main`. (You've probably noticed some of the effects of this, because you have things like `initialbalance += deposit;` in both functions.) – Nathan Pierson Nov 07 '22 at 00:10

1 Answers1

1

When you call DepositMoney(), you have this check: if (initialbalance < deposit). At this point, initialbalance is 10000, so if the amount is greater, then you cannot deposit it. Based on the logic, you likely wanted a check like that in withdrawMoney(), not in DepositMoney().

lorro
  • 10,687
  • 23
  • 36
  • ah yes, that makes sense.. I just thought about it, im depositing, so i wouldnt need an if statement. the only thing i needed was if(deposit <=0).. thank you so much, i fixed it – Ubaid Rahman Nov 07 '22 at 00:11