Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. /*
  2. ATM MACHINE PROGRAM BY CHRIS BROOKS, TYLER GREENE, & ETHAN WANG SO
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Only works in Cloud9 in a folder with "loginData.txt" & "userBalance.txt"
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. */
  7. #include <iostream>
  8. #include <fstream>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <iomanip>
  14.  
  15. using namespace std;
  16.  
  17. string bar = "===========================================\n";
  18.  
  19. // function prototypes
  20. void printIntroMenu();
  21. void printMainMenu();
  22. void start();
  23. bool login();
  24. void createAccount();
  25.  
  26.  
  27. // global variable (use this variable to store the user’s menu selection)
  28. string menuInput;
  29. string username;
  30. string pass;
  31.  
  32. // the main function
  33. int main() {
  34. // TO WRITE A WELCOME MESSAGE HERE
  35. printIntroMenu();
  36. cin >> menuInput;
  37.  
  38. if (menuInput == "l") {
  39. if (login() == true)
  40. start();
  41. else
  42. return 0;
  43. }
  44. else if (menuInput == "c")
  45. createAccount();
  46. else if (menuInput == "q")
  47. return 0;
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53.  
  54. void printIntroMenu() {
  55. cout << "Hi! Welcome to the ATM Machine!\n"
  56. << "Please select an option from the menu below:\n"
  57. << "l -> Login\n"
  58. << "c -> Create New Account\n"
  59. << "q -> Quit\n";
  60. }
  61.  
  62.  
  63.  
  64. void printMainMenu() {
  65. cout << "d -> Deposit Money\n"
  66. << "w -> Withdraw Money\n"
  67. << "x -> Exit\n";
  68.  
  69. }
  70.  
  71.  
  72.  
  73. void start() {
  74.  
  75. double balance;
  76. string currentLine;
  77. fstream myfile("userBalance.txt");
  78. for (int lineNum = 0; getline(myfile, currentLine) && lineNum < 5; lineNum++) {
  79. if (lineNum == 0)
  80. balance = stod(currentLine);
  81. }
  82. cout << fixed << showpoint << setprecision(2);
  83. cout << bar << "Your current balance is: $" << balance << endl << bar;
  84.  
  85. printMainMenu();
  86. cout << bar;
  87. cin >> menuInput;
  88.  
  89. if (menuInput == "d") {
  90. // DEPOSIT
  91. float depositAmt;
  92. cout << "How much money would you like to deposit?" << endl;
  93. cin >> depositAmt;
  94. balance = balance + depositAmt;
  95. cout << bar << "Your balance is now: $" << balance << endl << bar;
  96. myfile.close();
  97.  
  98. ofstream myfile;
  99. myfile.open ("userBalance.txt");
  100. myfile << balance;
  101. myfile.close();
  102. }
  103. else if (menuInput == "w") {
  104. // WITHDRAW
  105. float withdrawAmt;
  106. cout << "How much money would you like to withdraw?" << endl;
  107. cin >> withdrawAmt;
  108. if ((balance - withdrawAmt) < 0) {
  109. cout << "You don't have enough money in your account to make this transaction!" << endl;
  110. }
  111. else {
  112. balance = balance - withdrawAmt;
  113. }
  114. cout << bar << "Your balance is now: $" << balance << endl << bar;
  115. myfile.close();
  116.  
  117. ofstream myfile;
  118. myfile.open ("userBalance.txt");
  119. myfile << balance;
  120. myfile.close();
  121. }
  122. else if (menuInput == "x")
  123. cout << "Goodbye." << endl;
  124. }
  125.  
  126. //ifstream infile;
  127. //infile.open(“loginData.txt”);
  128.  
  129. void createAccount() {
  130. ofstream myfile;
  131. myfile.open ("loginData.txt");
  132.  
  133. cout << "Type in your username: ";
  134. cin >> username;
  135. myfile << "user: " << endl << username << endl;
  136. cout << "Type in your password: ";
  137. cin >> pass;
  138. myfile << "pass: " << endl << pass << endl;
  139. myfile.close();
  140.  
  141. myfile.open ("userBalance.txt");
  142. myfile << 0;
  143. myfile.close();
  144. }
  145.  
  146.  
  147.  
  148. bool login() {
  149.  
  150. cout << "Please print user id:\n";
  151. cin >> username;
  152. cout << "Please print your password:\n";
  153. cin >> pass;
  154.  
  155. ifstream infile("loginData.txt");
  156.  
  157. string currentLine;
  158. string filedUsername;
  159. string filedPassword;
  160.  
  161. for (int lineno = 0; getline(infile, currentLine) && lineno < 5; lineno++) {
  162. if (lineno == 1)
  163. filedUsername = currentLine;
  164. if (lineno == 3)
  165. filedPassword = currentLine;
  166. }
  167.  
  168. if (username == filedUsername && pass == filedPassword) {
  169. cout << endl << "Login successful." << endl;
  170. return true;
  171. }
  172. else {
  173. cout << endl << "Invalid username/password combination." << endl;
  174. return false;
  175. }
  176.  
  177. infile.close();
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement