Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*MUHAMMAD HILMI BIN KAMARUL'AZMI CONTROL STRUCTURE*/
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- bool usingIfElse();
- bool usingSwitch();
- bool usingWhile();
- bool usingDoWhile();
- bool usingFor();
- int main()
- {
- /*SOALAN 1*/
- /* ATM BANK MACHINE - CHECK ACCOUNT BALANCE*/
- usingIfElse();
- //usingSwitch();
- /*SOALAN 2*/
- /* MORSE CODE - TEXT TO MORSE CODE*/
- //usingWhile();
- //usingDoWhile();
- //usingFor();
- return 0;
- }
- bool usingIfElse()
- {
- vector<int> id {5111 , 5112 , 5113 };
- vector<int> pin {865279 , 983547, 125476};
- vector<float> balance {1005.00, 95.15 , 60.10 };
- int idEntered { 0 };
- int pinEntered { 0 };
- int accountID { 0 };
- cout << "============== BANK PUO ==============" << endl;
- cout << " USERID:";
- cin >> idEntered;
- cout << " PIN:";
- cin >> pinEntered;
- cout << "============== BANK PUO ==============" << endl;
- if (idEntered == id.at(0))
- {
- if (pinEntered == pin.at(0))
- {
- cout << "WELCOME BACK [" << id.at(0) << "]" << endl;
- cout << "Current balance: RM" << balance.at(0) << endl;
- accountID = 0;
- }
- else
- {
- cout << "ERROR : Wrong password!";
- return false;
- }
- }
- else if (idEntered == id.at(1))
- {
- if (pinEntered == pin.at(1))
- {
- cout << "WELCOME BACK [" << id.at(1) << "]" << endl;
- cout << "Current balance: RM" << balance.at(1) << endl;
- accountID = 1;
- }
- else
- {
- cout << "ERROR : Wrong password!";
- return false;
- }
- }
- else if (idEntered == id.at(2))
- {
- if (pinEntered == pin.at(2))
- {
- cout << "WELCOME BACK [" << id.at(2) << "]" << endl;
- cout << "Current balance: RM" << balance.at(2) << endl;
- accountID = 2;
- }
- else
- {
- cout << "ERROR : Wrong password!";
- return false;
- }
- }
- else { cout << "ERROR : There's no account with that username or password!"; return false; }
- int isWithdrawCash{ 0 };
- cout << "============== BANK PUO ==============" << endl;
- cout << "withdraw cash? [Y-1/N-2]:";
- cin >> isWithdrawCash;
- if (isWithdrawCash == 1)
- {
- float cashToWithdraw{ 0 };
- cout << "============== BANK PUO ==============" << endl;
- cout << "Cash to withdraw : RM";
- cin >> cashToWithdraw;
- if (cashToWithdraw < balance.at(accountID))
- {
- balance.at(accountID) -= cashToWithdraw;
- cout << "============== BANK PUO ==============" << endl;
- cout << "HELLO USER [" << id.at(accountID) << "]" << endl;
- cout << "Current balance: RM" << balance.at(accountID) << endl;
- }
- else { cout << "ERROR : Not enough balance"; return false; }
- }
- else { cout << "GOODBYE! PLEASE COME AGAIN"; return false; }
- return false;
- }
- bool usingSwitch()
- {
- vector<int> id { 5111 , 5112 , 5113 };
- vector<int> pin { 865279 , 983547, 125476 };
- vector<float> balance{ 1005.00, 95.15 , 60.10 };
- int idEntered{ 0 };
- int pinEntered{ 0 };
- int accountID{ 0 };
- cout << "============== BANK PUO ==============" << endl;
- cout << " USERID:";
- cin >> idEntered;
- cout << " PIN:";
- cin >> pinEntered;
- cout << "============== BANK PUO ==============" << endl;
- switch (idEntered)
- {
- case 5111:
- switch (pinEntered)
- {
- case 865279:
- cout << "WELCOME BACK [" << id.at(0) << "]" << endl;
- cout << "Current balance: RM" << balance.at(0) << endl;
- accountID = 0;
- break;
- default:
- cout << "ERROR : Wrong password!";
- return false;
- break;
- }
- break;
- case 5112:
- switch (pinEntered)
- {
- case 983547:
- cout << "WELCOME BACK [" << id.at(1) << "]" << endl;
- cout << "Current balance: RM" << balance.at(1) << endl;
- accountID = 1;
- break;
- default:
- cout << "ERROR : Wrong password!";
- return false;
- break;
- }
- break;
- case 5113:
- switch (pinEntered)
- {
- case 125476:
- cout << "WELCOME BACK [" << id.at(2) << "]" << endl;
- cout << "Current balance: RM" << balance.at(2) << endl;
- accountID = 2;
- break;
- default:
- cout << "ERROR : Wrong password!";
- return false;
- break;
- }
- break;
- default:
- cout << "ERROR : There's no account with that username or password!";
- return false;
- break;
- }
- int isWithdrawCash{ 0 };
- cout << "============== BANK PUO ==============" << endl;
- cout << "withdraw cash? [Y-1/N-2]:";
- cin >> isWithdrawCash;
- float cashToWithdraw{ 0 };
- switch (isWithdrawCash)
- {
- case 1:
- cout << "============== BANK PUO ==============" << endl;
- cout << "Cash to withdraw : RM";
- cin >> cashToWithdraw;
- if (cashToWithdraw < balance.at(accountID))
- {
- balance.at(accountID) -= cashToWithdraw;
- cout << "============== BANK PUO ==============" << endl;
- cout << "HELLO USER [" << id.at(accountID) << "]" << endl;
- cout << "Current balance: RM" << balance.at(accountID) << endl;
- }
- else { cout << "ERROR : Not enough balance"; return false; }
- break;
- default:
- cout << "GOODBYE! PLEASE COME AGAIN";
- return false;
- break;
- }
- return false;
- }
- bool usingWhile()
- {
- bool isLoop{ true };
- while (isLoop)
- {
- cout << "This is a english to morse code translator please seperate each sentence using " << endl;
- cout << "Enter your text :";
- string text;
- getline(cin, text);
- vector <char> data{
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' , 'k' , 'l' , 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', char(32)
- };
- vector <string> morse{
- ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
- "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
- };
- vector <string> textOut(text.length());
- int numbX{ -1 };
- for (unsigned int i{ 1 }; i <= text.length(); ++i) {
- ++numbX;
- int numb = -1;
- do { ++numb; } while (data.at(numb) != text.at(numbX));
- textOut.at(numbX) = morse.at(numb);
- cout << textOut.at(numbX) << " ";
- } cout << endl;
- char isEnd{ 'Y' };
- cout << "Do you want to end? [Y/N] :";
- cin >> isEnd;
- if (!(isEnd == 'Y'))
- {
- cout << "GOODBYE!"; isLoop = false;
- }
- cin.ignore();
- }
- return false;
- }
- bool usingDoWhile()
- {
- bool isLoop{ true };
- cout << "This is a english to morse code translator please seperate each sentence using " << endl;
- cout << "Enter your text :";
- do
- {
- string text;
- getline(cin, text);
- vector <char> data{
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' , 'k' , 'l' , 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', char(32)
- };
- vector <string> morse{
- ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
- "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
- };
- vector <string> textOut(text.length());
- int numbX{ -1 };
- for (unsigned int i{ 1 }; i <= text.length(); ++i) {
- ++numbX;
- int numb = -1;
- do { ++numb; } while (data.at(numb) != text.at(numbX));
- textOut.at(numbX) = morse.at(numb);
- cout << textOut.at(numbX) << " ";
- } cout << endl;
- char isEnd{ 'Y' };
- cout << "Do you want to end? [Y/N] :";
- cin >> isEnd;
- if (!(isEnd == 'Y'))
- {
- cout << "GOODBYE!"; isLoop = false;
- }
- cin.ignore();
- } while (isLoop);
- return false;
- }
- bool usingFor()
- {
- cout << "This is a english to morse code translator please seperate each sentence using " << endl;
- cout << "Enter your text :";
- string text;
- getline(cin, text);
- vector <char> data{
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' , 'k' , 'l' , 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', char(32)
- };
- vector <string> morse{
- ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
- "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
- };
- vector <string> textOut(text.length());
- int numbX{ -1 };
- for (unsigned int i{ 1 }; i <= text.length(); ++i) {
- ++numbX;
- int numb = -1;
- do { ++numb; } while (data.at(numb) != text.at(numbX));
- textOut.at(numbX) = morse.at(numb);
- cout << textOut.at(numbX) << " ";
- } cout << endl;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement