hai. anyone can help me ? I run my coding but it be invalid option even I already declare my coding. Can I know which part I wrong ?

#include <iostream>
#include <string>

using namespace std;

void printintromenu();
void login();
void mainmenu();

int main ()
{

    cout << "Welcome To ZS ATM" << endl << endl;
    char intro;
    cout << " ******************Welcome to ZS's ATM Machine*****************" << endl;
    cout << "l) Login" << endl;
    cout << "q) Quit" << endl;
    cin >> intro;

    if (intro=='l')
    {
        login();
        mainmenu();
    }
    else if (intro=='q')
    {
        cout << "You have quit the program" << endl;

    }
    mainmenu();
    return 0;

}

void login ()
    {
        string id;
        string password;
        bool loginSuccess = false;

        do
        {
            cout << "\n\nPlease enter your user id: " << endl;
            cin >> id;
            cout << "\n\nPlease enter your password: " << endl;
            cin >> password;

            if (id == "zurena" && password == "zurena")
            {
                cout << " Successful Login ";
                loginSuccess = true;
            }

            else 
            {
                cout << "**********LOGIN FAILED!**********\n\n";
                cout << "Please try to login again" << endl;
            }   

    }

    while (!loginSuccess);
    }

void mainmenu ()    
{
    char option;
    float balance, amount,withdraw;
    balance=0;
    //Prompting user to select account type
    cout << "Access Granted ! " << endl;
   //Display account operations options to user
     cout << "d) Deposit Money" << endl;
     cout << "w) Withdraw Money" << endl;
     cout << "r) Request Balance" << endl;
     cout << "q) Quit" << endl;
     cout << "Select an option: ";
     cin >> option;

   //prompt user to select operation
    while (option!='d'|| option!='w' || option!='r' ) 
    {
        if (option == 'd') // User want to Deposit money
        {
            cout << "Please enter amount to deposit ";
            cin >> amount;
            balance=balance+amount;
            cout << " The deposit amount you had entered is: RM" << amount << endl;
        }
        else if (option=='r')
        {
            cout << "Your balance is: RM " << balance << endl;
        }   

        else if(option == 'w') // User want to Withdraw money
        {
           //prompt user to enter amount he/she want to Withdraw money
           cout << "Please enter amount to withdraw ";
           cin >> withdraw;
           balance=balance-withdraw;
            cout << "You had withdraw: RM" << withdraw << endl;
        }

        cout << "Invalid Option Selected. Please select the option given" << endl;
        cin >> option;
    }

}

Recommended Answers

All 2 Replies

1) It would be nice to have a do-loop around your login function so the user will be forced to login or quit
2) Inside "mainMenu()" the test for the option should be shortend to JUST ensure a correct choice is made. Once made, the program can drop down to respond to what the user chose.
3) The floats "amount" and "withdraw" should be moved inside the clauses that use them.
4) The float "balance" should be made static (this would work as long as you will only allow one user per run). This will allow it to retain value for balance checking later on.
5) Please initialize all of your variables to default values. This will aid in debugging.

There is more, but if you make these changes, some of the other ones will reveal themselves.

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.