Advertisement
enkacang

CONTROL STRUCTURE

Apr 21st, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. /*MUHAMMAD HILMI BIN KAMARUL'AZMI CONTROL STRUCTURE*/
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. bool usingIfElse();
  9. bool usingSwitch();
  10. bool usingWhile();
  11. bool usingDoWhile();
  12. bool usingFor();
  13.  
  14. int main()
  15. {
  16.     /*SOALAN 1*/
  17.     /* ATM BANK MACHINE - CHECK ACCOUNT BALANCE*/
  18.  
  19.     usingIfElse();
  20.     //usingSwitch();
  21.  
  22.     /*SOALAN 2*/
  23.     /* MORSE CODE - TEXT TO MORSE CODE*/
  24.  
  25.     //usingWhile();
  26.     //usingDoWhile();
  27.     //usingFor();
  28.  
  29.     return 0;
  30. }
  31.  
  32. bool usingIfElse()
  33. {
  34.     vector<int> id        {5111   ,  5112  ,   5113  };
  35.     vector<int> pin       {865279 ,  983547,   125476};
  36.     vector<float> balance {1005.00,  95.15 ,   60.10 };
  37.  
  38.     int idEntered { 0 };
  39.     int pinEntered { 0 };
  40.     int accountID { 0 };
  41.  
  42.     cout << "============== BANK PUO ==============" << endl;
  43.     cout << "        USERID:";
  44.     cin >> idEntered;
  45.     cout << "           PIN:";
  46.     cin >> pinEntered;
  47.     cout << "============== BANK PUO ==============" << endl;
  48.  
  49.     if (idEntered == id.at(0))
  50.     {
  51.         if (pinEntered == pin.at(0))
  52.         {
  53.             cout << "WELCOME BACK [" << id.at(0) << "]" << endl;
  54.             cout << "Current balance: RM" << balance.at(0) << endl;
  55.             accountID = 0;
  56.         }
  57.         else
  58.         {
  59.             cout << "ERROR : Wrong password!";
  60.             return false;
  61.         }
  62.     }
  63.     else if (idEntered == id.at(1))
  64.     {
  65.         if (pinEntered == pin.at(1))
  66.         {
  67.             cout << "WELCOME BACK [" << id.at(1) << "]" << endl;
  68.             cout << "Current balance: RM" << balance.at(1) << endl;
  69.             accountID = 1;
  70.         }
  71.         else
  72.         {
  73.             cout << "ERROR : Wrong password!";
  74.             return false;
  75.         }
  76.     }
  77.     else if (idEntered == id.at(2))
  78.     {
  79.         if (pinEntered == pin.at(2))
  80.         {
  81.             cout << "WELCOME BACK [" << id.at(2) << "]" << endl;
  82.             cout << "Current balance: RM" << balance.at(2) << endl;
  83.             accountID = 2;
  84.         }
  85.         else
  86.         {
  87.             cout << "ERROR : Wrong password!";
  88.             return false;
  89.         }
  90.     }
  91.     else { cout << "ERROR : There's no account with that username or password!"; return false; }
  92.  
  93.     int isWithdrawCash{ 0 };
  94.     cout << "============== BANK PUO ==============" << endl;
  95.     cout << "withdraw cash? [Y-1/N-2]:";
  96.     cin >> isWithdrawCash;
  97.  
  98.     if (isWithdrawCash == 1)
  99.     {
  100.         float cashToWithdraw{ 0 };
  101.         cout << "============== BANK PUO ==============" << endl;
  102.         cout << "Cash to withdraw : RM";
  103.         cin >> cashToWithdraw;
  104.  
  105.         if (cashToWithdraw < balance.at(accountID))
  106.         {
  107.  
  108.             balance.at(accountID) -= cashToWithdraw;
  109.  
  110.             cout << "============== BANK PUO ==============" << endl;
  111.             cout << "HELLO USER [" << id.at(accountID) << "]" << endl;
  112.             cout << "Current balance: RM" << balance.at(accountID) << endl;
  113.  
  114.         }
  115.         else { cout << "ERROR : Not enough balance"; return false; }
  116.  
  117.     }
  118.     else { cout << "GOODBYE! PLEASE COME AGAIN"; return false; }
  119.    
  120.     return false;
  121. }
  122.  
  123. bool usingSwitch()
  124. {
  125.     vector<int> id       { 5111   ,  5112  ,   5113 };
  126.     vector<int> pin      { 865279 ,  983547,   125476 };
  127.     vector<float> balance{ 1005.00,  95.15 ,   60.10 };
  128.  
  129.     int idEntered{ 0 };
  130.     int pinEntered{ 0 };
  131.     int accountID{ 0 };
  132.  
  133.     cout << "============== BANK PUO ==============" << endl;
  134.     cout << "        USERID:";
  135.     cin >> idEntered;
  136.     cout << "           PIN:";
  137.     cin >> pinEntered;
  138.     cout << "============== BANK PUO ==============" << endl;
  139.  
  140.     switch (idEntered)
  141.     {
  142.     case 5111:
  143.         switch (pinEntered)
  144.         {
  145.         case 865279:
  146.             cout << "WELCOME BACK [" << id.at(0) << "]" << endl;
  147.             cout << "Current balance: RM" << balance.at(0) << endl;
  148.             accountID = 0;
  149.             break;
  150.         default:
  151.             cout << "ERROR : Wrong password!";
  152.             return false;
  153.             break;
  154.         }
  155.         break;
  156.     case 5112:
  157.         switch (pinEntered)
  158.         {
  159.         case 983547:
  160.             cout << "WELCOME BACK [" << id.at(1) << "]" << endl;
  161.             cout << "Current balance: RM" << balance.at(1) << endl;
  162.             accountID = 1;
  163.             break;
  164.         default:
  165.             cout << "ERROR : Wrong password!";
  166.             return false;
  167.             break;
  168.         }
  169.         break;
  170.     case 5113:
  171.         switch (pinEntered)
  172.         {
  173.         case 125476:
  174.             cout << "WELCOME BACK [" << id.at(2) << "]" << endl;
  175.             cout << "Current balance: RM" << balance.at(2) << endl;
  176.             accountID = 2;
  177.             break;
  178.         default:
  179.             cout << "ERROR : Wrong password!";
  180.             return false;
  181.             break;
  182.         }
  183.         break;
  184.     default:
  185.         cout << "ERROR : There's no account with that username or password!";
  186.         return false;
  187.         break;
  188.     }
  189.  
  190.     int isWithdrawCash{ 0 };
  191.     cout << "============== BANK PUO ==============" << endl;
  192.     cout << "withdraw cash? [Y-1/N-2]:";
  193.     cin >> isWithdrawCash;
  194.  
  195.     float cashToWithdraw{ 0 };
  196.     switch (isWithdrawCash)
  197.     {
  198.     case 1:
  199.         cout << "============== BANK PUO ==============" << endl;
  200.         cout << "Cash to withdraw : RM";
  201.         cin >> cashToWithdraw;
  202.  
  203.         if (cashToWithdraw < balance.at(accountID))
  204.         {
  205.  
  206.             balance.at(accountID) -= cashToWithdraw;
  207.  
  208.             cout << "============== BANK PUO ==============" << endl;
  209.             cout << "HELLO USER [" << id.at(accountID) << "]" << endl;
  210.             cout << "Current balance: RM" << balance.at(accountID) << endl;
  211.  
  212.         }
  213.         else { cout << "ERROR : Not enough balance"; return false; }
  214.         break;
  215.     default:
  216.         cout << "GOODBYE! PLEASE COME AGAIN";
  217.         return false;
  218.         break;
  219.     }
  220.  
  221.     return false;
  222. }
  223.  
  224. bool usingWhile()
  225. {
  226.     bool isLoop{ true };
  227.  
  228.     while (isLoop)
  229.     {
  230.         cout << "This is a english to morse code translator please seperate each sentence using " << endl;
  231.         cout << "Enter your text :";
  232.  
  233.         string text;
  234.  
  235.         getline(cin, text);
  236.  
  237.         vector <char> data{
  238.  
  239.         '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)
  240.         };
  241.  
  242.         vector <string> morse{
  243.  
  244.         ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
  245.         "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
  246.  
  247.         };
  248.  
  249.         vector <string> textOut(text.length());
  250.  
  251.         int numbX{ -1 };
  252.         for (unsigned int i{ 1 }; i <= text.length(); ++i) {
  253.             ++numbX;
  254.             int numb = -1;
  255.  
  256.             do { ++numb; } while (data.at(numb) != text.at(numbX));
  257.  
  258.             textOut.at(numbX) = morse.at(numb);
  259.             cout << textOut.at(numbX) << " ";
  260.  
  261.         } cout << endl;
  262.  
  263.         char isEnd{ 'Y' };
  264.         cout << "Do you want to end? [Y/N] :";
  265.         cin >> isEnd;
  266.  
  267.         if (!(isEnd == 'Y'))
  268.         {
  269.             cout << "GOODBYE!"; isLoop = false;
  270.         }
  271.  
  272.         cin.ignore();
  273.     }
  274.  
  275.     return false;
  276. }
  277.  
  278. bool usingDoWhile()
  279. {
  280.     bool isLoop{ true };
  281.  
  282.     cout << "This is a english to morse code translator please seperate each sentence using " << endl;
  283.     cout << "Enter your text :";
  284.  
  285.     do
  286.     {
  287.         string text;
  288.  
  289.         getline(cin, text);
  290.  
  291.         vector <char> data{
  292.  
  293.         '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)
  294.         };
  295.  
  296.         vector <string> morse{
  297.  
  298.         ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
  299.         "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
  300.  
  301.         };
  302.  
  303.         vector <string> textOut(text.length());
  304.  
  305.         int numbX{ -1 };
  306.         for (unsigned int i{ 1 }; i <= text.length(); ++i) {
  307.             ++numbX;
  308.             int numb = -1;
  309.  
  310.             do { ++numb; } while (data.at(numb) != text.at(numbX));
  311.  
  312.             textOut.at(numbX) = morse.at(numb);
  313.             cout << textOut.at(numbX) << " ";
  314.  
  315.         } cout << endl;
  316.  
  317.         char isEnd{ 'Y' };
  318.         cout << "Do you want to end? [Y/N] :";
  319.         cin >> isEnd;
  320.        
  321.         if (!(isEnd == 'Y'))
  322.         {
  323.             cout << "GOODBYE!"; isLoop = false;
  324.         }
  325.  
  326.         cin.ignore();
  327.  
  328.     } while (isLoop);
  329.  
  330.     return false;
  331. }
  332.  
  333. bool usingFor()
  334. {
  335.     cout << "This is a english to morse code translator please seperate each sentence using " << endl;
  336.     cout << "Enter your text :";
  337.     string text;
  338.  
  339.     getline(cin, text);
  340.  
  341.     vector <char> data{
  342.     '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)
  343.     };
  344.  
  345.     vector <string> morse{
  346.     ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...", "..", ".---", "-.-", ".-..", "--",
  347.     "-.", "---", ".--.", "--.-", ".-.","...","-", "..-","...-", ".--", "-..-", "-.--","--..","/"
  348.     };
  349.  
  350.     vector <string> textOut(text.length());
  351.  
  352.     int numbX{ -1 };
  353.     for (unsigned int i{ 1 }; i <= text.length(); ++i) {
  354.         ++numbX;
  355.         int numb = -1;
  356.  
  357.         do { ++numb; } while (data.at(numb) != text.at(numbX));
  358.  
  359.         textOut.at(numbX) = morse.at(numb);
  360.         cout << textOut.at(numbX) << " ";
  361.  
  362.     } cout << endl;
  363.  
  364.     return false;
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement