//The ATM Mahcine
#include <iostream>
using namespace std;
int main (const int)
{
int cash;
int bank;
int choice2;
int choice3;
char choice1;

cout << "please enter amoount of $ in bank: ";
cin >> bank;
cout << "please enter amount of $ in cash(hand/wallet): ";
cin >> cash;
cout << "You have " << bank << "$ in your bank.\n";
cout << "You have "<< cash << "$ in your wallet.\n";
cout << "Deposit or Withdraw?";
cin >> choice1;
if ( choice1 = choice1 )
{
cout << "How much will you deposit?\n";
cin >> choice2;
if ( choice2 <= cash )
cout << "you deposit " << choice2 << "$";
else
cout << "Insufficiant Funds...";
return 0;
}
else
{ 
cout << "How much will you withdraw?";
cin >> choice3;
if ( choice3 <= bank )
cout << "you withdraw " << choice3 << "$";
else
cout << "Insuffciant Funds...";
return 0;
}
return 0;
}

when i run this code on Visual C++ 2005 Express Edition,
the code dosn't give the user a choice to input deposit or withdraw,
it just automaicly goes to deposit and displays what the user inputed as the variable "cash".
PLEASE HELP ME FIX THIS PROGRAM!!!!!!!!!

Recommended Answers

All 4 Replies

What exactly is this line for?

if ( choice1 = choice1 )

Basically it's doing nothing. For one, it looks like you were trying to check a value, except that you need a ==. And for another, you aren't even comparing the variable with anything.

Perhaps you want to check this value with 'd' or 'w' to indicate deposit or withdrawl. (And you should document these values in the program's output; the user has no way of knowing what to type in.)

if ( choice1 = choice1 )

This will always be a true statement for two reasons. Firstly, the == operator is used to compare things, the = operator sets values. Secondly, comparing choice1 with choice1 is sure to be true for our universe. ;) You probably meant to use something like 'W' or 'D'. In that case, you might follow a simple pattern.

#include <ios>
#include <iostream>
#include <limits>


int main()
{
  using namespace std;

  char choice;
  bool done = false;

  while ( !done ) {
    // Get the user's choice
    cin.get( choice );

    // Select the choice based on any cases
    switch ( choice ) {
      case 'W':
        // Code for withdrawl
        break;
      case 'D':
        // Code for deposit
        break;
      case 'Q':
        // Code for cleanup
        done = true;
      default:
        // Code for handling unknown choices
        break;
    }

    // Clean up extra stream characters
    cin.clear();
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  }

  return 0;
}

well, i meant for it to be so that where it said
if ( choice1 = choice1 )to mean that when it saidchar choice1;i could make it so maybe it would look like this so when the typed: deposit, it would goto the deposit part of the if/else statment char choice1 = "deposit";

Nope, that's not how it works. You're going to have to implement something similar to what Ravalon mentioned. If you wanted to take an entire string instead of just 1 letter, you'd have to firstly have choice1 as a char array, and secondly you'd have to implement if() statements using strcmp() to compare the value inside choice1 to a literal, such as "deposit".

e.g.

char choice1[20];
// ....
if (strcmp(choice1, "deposit") == 0) {
    // user entered deposit
}

else if (strcmp(choice1, "withdraw") == 0) {
   // user entered withdraw
}

// etc.

And by the way, perhaps you should choose more descriptive names for your variables rather than "choicex". It becomes hard to tell which variable is for which part of the program.

Believe me: you can't compare a variable with itself. It just doesn't do anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.