944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7055
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 21st, 2005
-1

bank account

Expand Post »
Hi, I have a big project (#1). this is its direction:
Project I

Software for a Bank

Write a C++ program for the following task:
a) Should allow 5 users with user name & password [ (Ohlone1, Ohlone1pw), (Ohlone2, Ohlone2pw),( Ohlone3, Ohlone3pw), (Ohlone4, Ohlone4pw), (Ohlone5, Ohlone5pw)]

b) Display a welcome / greeting .

c) Menu driven program to allow ‘View Balance’, ‘Deposit’, ‘Withdraw’, ‘Transfer’ and ‘Exit’. Start with a beginning balance of 1000.00 in checking and 1500.00 in savings.

These are the general specifications, use your creativity to make program user friendly.


So far I've laid out the general scheme in the following code. But I have no idea how to do the depositing, withdrawing, or the transfering...worse yet...How can you show these amounts on the "view Balance"?
Do I have to repeat this for each and every user? Is there a simpler way to simplify this process?

C++ Syntax (Toggle Plain Text)
  1. //Term Project #1 - 100 points
  2. //SOFTWARE FOR A BANK
  3. //Write a C++ program for the following task using modular approach.
  4. //a) Should allow 5 users with user name & password :
  5. //[ (Ohlone1, Ohlone1pw), (Ohlone2, Ohlone2pw),( Ohlone3, Ohlone3pw), (Ohlone4, Ohlone4pw), (Ohlone5, Ohlone5pw)]
  6. //b) Display a welcome / greeting .
  7. //c) Menu driven program to allow:
  8. //‘View Balance’,
  9. //‘Deposit’,
  10. //‘Withdraw’,
  11. //‘Transfer’
  12. //and ‘Exit’.
  13. //Start with a beginning balance of 1000.00 in checking and 1500.00 in savings.
  14.  
  15. #include <iostream.h>
  16. #include <string.h>
  17.  
  18. //PROTOTYPE;
  19. void Signin();
  20. void Menu1();
  21. void ViewBalance1();
  22. void Ohlone1_balance();
  23. void Ohlone1_Deposit();
  24. void Ohlone1_Withdraw();
  25. void Ohlone1_Transfer();
  26. void Ohlone1_Checking();
  27. void Ohlone1_Saving();
  28.  
  29.  
  30. void main (void)
  31. {
  32.  
  33. cout << "WELCOME TO PROGRAMMING BANK!\n\n";
  34. cout << "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
  35. Signin ();
  36. Menu1 ();
  37. }
  38.  
  39. void Signin ()
  40. {
  41. char username[31], PW[20];
  42.  
  43. cout << "Please enter your username: \n";
  44. cin.width(32);
  45. cin >> username;
  46. do
  47. {
  48. //User 1
  49. if (strcmp(username, "Ohlone1") ==0)
  50. {
  51. cout << "Please enter your password: \n";
  52. cin >> PW;
  53. for (int i = 0; i < PW; i++)
  54. {
  55. cout << "*"; //How come this technique deosn't work?
  56. }
  57. if (strcmp(PW, "Ohlone1pw") == 0)
  58. cout << "\n\n\tYou are logged in!\n\t";
  59. else
  60. {
  61. cout << "Your password was entered incorrectly.\n";
  62. cout << "Please enter your password again.\n";
  63. cin >> PW;
  64. if (strcmp(PW, "Ohlone1pw") == 0)
  65. cout << "\n\n\tYou are logged in!\n\n";
  66. else
  67. {
  68. cout << "I'm sorry you're having trouble.\n";
  69. break;
  70. }
  71. }
  72. return;
  73. }
  74. //User 2
  75. else if (strcmp(username, "Ohlone2") ==0)
  76. {
  77. cout << "Please enter your password: \n";
  78. cin >> PW;
  79. if (strcmp(PW, "Ohlone2pw") == 0)
  80. cout << "\n\n\tYou are logged in!\n\n";
  81. else
  82. {
  83. cout << "Your password was entered incorrectly.\n";
  84. cout << "Please enter your password again.\n";
  85. cin >> PW;
  86. if (strcmp(PW, "Ohlone2pw") == 0)
  87. cout << "\n\n\tYou are logged in!\n\n";
  88. else
  89. {
  90. cout << "I'm sorry you're having trouble.\n";
  91. break;
  92. }
  93. }
  94. return;
  95. }
  96. //User 3
  97. else if (strcmp(username, "Ohlone3") == 0)
  98. {
  99. cout << "Please enter your password: \n";
  100. cin >> PW;
  101. if (strcmp(PW, "Ohlone3pw") == 0)
  102. cout << "\n\n\tYou are logged in!\n\n";
  103. else
  104. {
  105. cout << "Your password was entered incorrectly.\n";
  106. cout << "Please enter your password again.\n";
  107. cin >> PW;
  108. if (strcmp(PW, "Ohlone3pw") == 0)
  109. cout << "\n\n\tYou are logged in!\n\n";
  110. else
  111. {
  112. cout << "I'm sorry you're having trouble.\n";
  113. break;
  114. }
  115. }
  116. return;
  117. }
  118. //User 4
  119. else if (strcmp(username, "Ohlone4") == 0)
  120. {
  121. cout << "Please enter your password: \n";
  122. cin >> PW;
  123. if (strcmp(PW, "Ohlone4pw") == 0)
  124. cout << "\n\n\tYou are logged in!\n\n";
  125. else
  126. {
  127. cout << "Your password was entered incorrectly.\n";
  128. cout << "Please enter your password again.\n";
  129. cin >> PW;
  130. if (strcmp(PW, "Ohlone4pw") == 0)
  131. cout << "\n\n\tYou are logged in!\n";
  132. else
  133. {
  134. cout << "I'm sorry you're having trouble.\n";
  135. break;
  136. }
  137. }
  138. return;
  139. }
  140. //User 5
  141. else if (strcmp(username, "Ohlone5") == 0)
  142. {
  143. cout << "Please enter your password: \n";
  144. cin >> PW;
  145. if (strcmp(PW, "Ohlone5pw") == 0)
  146. cout << "\n\n\tYou are logged in!\n\n";
  147. else
  148. {
  149. cout << "Your password was entered incorrectly.\n";
  150. cout << "Please enter your password again.\n";
  151. cin >> PW;
  152. if (strcmp(PW, "Ohlone5pw") == 0)
  153. cout << "\n\n\tYou are logged in!\n\n";
  154. else
  155. {
  156. cout << "I'm sorry you're having trouble.\n";
  157. break;
  158. }
  159. }
  160. return;
  161. }
  162. }while (strcmp(username, "Ohlone1") == 0 || (username, "Ohlone2") == 0 ||
  163. (username, "Ohlone3") == 0 || (username, "Ohlone4") ==0 || (username, "Ohlone5") == 0);
  164.  
  165. if (strcmp(username, "Ohlone1") != 0 || (username, "Ohlone2") != 0 ||
  166. (username, "Ohlone3") != 0 || (username, "Ohlone4") !=0 || (username, "Ohlone5") != 0)
  167. {
  168. cout << "your enter your username incorrectly.\n";
  169. cout << "Please try again.\n";
  170. }
  171. return;
  172. }
  173. void Menu1 ()
  174. {
  175. int choice;
  176.  
  177. cout << "\nWHAT WOULD YOU LIKE TO DO?\n";
  178. cout << "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n";
  179. cout << "\t1. View Your Balance.\n";
  180. cout << "\t2. Deposit.\n";
  181. cout << "\t3. Withdraw.\n";
  182. cout << "\t4. Transfer.\n";
  183. cout << "\t5. Exit.\n\n";
  184. cout << "Please enter your choice: \n";
  185. cin >> choice;
  186. if (choice ==1)
  187. {
  188. Ohlone1_balance();
  189. }
  190. else if (choice ==2)
  191. {
  192. Ohlone1_Deposit();
  193. }
  194. else if (choice ==3)
  195. {
  196. Ohlone1_Withdraw();
  197. }
  198. else if (choice ==4)
  199. {
  200. Ohlone1_Transfer();
  201. }
  202. else if (choice ==5)
  203. {
  204. cout << "You have chosen to exit the menu.\n";
  205. cout << "Thank you for visiting Programming Bank.\n";
  206. return;
  207. }
  208. }
  209.  
  210. void Ohlone1_balance ()
  211. {
  212. //cout << "Your checking account has : " << << endl;
  213. //cout << "Your saving account has : " << << endl;
  214. }
  215.  
  216. void Ohlone1_Checking (float *amount)
  217. {
  218. //amount = 1000;
  219.  
  220. }
  221.  
  222. void Ohlone1_Saving (float *amount)
  223. {
  224. //amount = 1500;
  225. }
  226.  
  227. void Ohlone1_Deposit ()
  228. {
  229. int input;
  230. float deposit;
  231.  
  232. cout << "Which account would you like to deposit to?\n";
  233. cout << "1. Checking Account\n";
  234. cout << "2. Saving Account\n";
  235. cin >> input;
  236. if (input = 1)
  237. {
  238. cout << "How much do you want to deposit to the Checking Account?\n";
  239. cin >> deposit;
  240. }
  241. if (input =2)
  242. {
  243. cout << "How much do you want to deposit to the Saving Account?\n";
  244. cin >> deposit;
  245. }
  246. }
  247.  
  248. void Ohlone1_Transfer ()
  249. {
  250. int input;
  251.  
  252. cout << "Would you like to transfer..\n\n";
  253. cout << "1. From CHECKING to SAVING\n";
  254. cout << "\nOR\n\n";
  255. cout << "2. From SAVING to CHECKING\n";
  256. cin >> input;
  257. if (input = 1)
  258. {
  259. // C_to_S ();
  260. }
  261. if (input = 2)
  262. {
  263. // S_to_C ();
  264. }
  265. }
  266.  
  267. void Ohlone1_Withdraw ()
  268. {
  269. int input;
  270. float withdraw;
  271.  
  272. cout << "From which account would you like to withdraw?\n";
  273. cout << "1. Checking\n";
  274. cout << "2. Saving\n";
  275. cin >> input;
  276. if (input = 1)
  277. {
  278. cout << "How much would you like to withdraw from the Checking account?\n";
  279. cin >> withdraw;
  280. //what to do here?
  281. }
  282. if (input = 2)
  283. {
  284. cout << "How much would you like to withdraw from the Saving account?\n";
  285. cin >> withdraw;
  286. //what to do here?
  287. }
  288. }

I know it's really long. But can you guys help please?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

Did you learn about structs or classes recently?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

umm...no...I'm a level-1 beginner.
You can tell from my coding that I'm pretty much clueless in C++.
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

This is assignment. (The subsequent comparison is always true.)
C++ Syntax (Toggle Plain Text)
  1. if (input = 1)
This is comparison.
if (input == 1)
Ever here the phrase, "What happens in Vegas stays in Vegas" (or something similar)? Well, function are like that too. Whatever happens to a local variable inside a function is lost on the outside of that function. So if you want to keep track of something, you would want to be passing parameters to a function and returning values from them to "communicate" with one another.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 21st, 2005
0

Re: bank account

Just outta curiosity, are you learning this stuff in a class, or from a book?

And everyone starts at level 1.

I'm just asking so that you go down the right path for the class, but if it's from a book and this is just for your own betterment, I'll show you how to do these things with structs and classes.

Aaaanyway: For your concerns about deposits, withdrawals, and transfers:
You should have two variables for monetary values. Savings and checking, let's call 'em.
What you do is you declare and initialize them like so:

C++ Syntax (Toggle Plain Text)
  1. float checking = 1000.;
  2. float savings = 1500.;

and that gives you the initial values.

When you want to make a deposit, you need to specify checking or savings, and then add the appropriate amount (from user input) to the appropriate account.

Making a withdrawal is a little tougher. Once you've specified savings or checking, you need to make sure that the user doesn't overdraw their account. That means you have to use an 'if' statement to check that the amount of money to be withdrawn is less than or equal to the current value held in the account.


What you'd want is something like this:
C++ Syntax (Toggle Plain Text)
  1. float deposit(int account_type);
  2. //Asks for amount to add, and returns the new value of the appropriate account.
  3.  
  4. float withdraw(int account_type);
  5. //Asks, Error checks the balance, returns the value of the balance if transaction is good. Otherwise, returns a -1.0

The transfers, though... That's a little odd.
You can't return two values from a function, you have to use pointers...
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

uh...to answer your question, Drowzee, I'm taking this introduction to C++ class in a college (ohlone, if you know the name) right now and it's in the summer. the class is normall 3 months long but since it's in the summer, it's only 6 weeks! WAHHH!! But the same materials have to be covered, so it's like 3 chapters a week! So even though, I'm taking the class, I'm basically on my own 'cause the teacher barely has time for lectures, let alone helping individual students (although all of my classmates seem to know what they're doing, I'm always the last one to turn in something, even tests).That's why I'm constantly asking for help. It took forever to figure out what I did wrong, or just to figure out what to do!
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

As I didn't see any rules against double posting in this fashion, I thought it best to split this off to help seperate the topic, as my last one was far too heavily edited and almost unusable.

Yes, your current code is far too redundant. If you ever have code that is repeated with only minor changes like the name entered, there's a better, faster way to do it.

Let me introduce you to your friend and mine, 'Switch'.
Switch is, for an integer input (like a menu choice), a very handy tool.

C++ Syntax (Toggle Plain Text)
  1.  
  2. int menu_sel;
  3.  
  4. //Get your menu selection somehow.
  5.  
  6. switch(menu_sel){
  7. case 0: //menu selection for, I dunno... Deposits.
  8. Your deposit function;
  9. case 1: //Let's say, withdrawals.
  10. your withdraw function;
  11. case 2: //for whatever else you want. Just keep adding cases.
  12. default: //This must always be at the end of the cases. It catches anything that wasn't supposed to come through. So, if someone enters G, it'll be caught here.
  13. break; //It's always a good idea to use 'break' in the default. Just do it, it's good, safe coding.
  14. } //end of the switch statement.

That'll help a little. More importantly, you'll need to make it so that you have one generic login, not five. You can do this a number of ways, but the lowest-level one would be to have two arrays of strings, . One for the names, one for the associated passwords.
Also, two arrays (or one 2-D array) of floats for the balances.

You do the log in by asking for a string, and iterate through the name array to find a match with strcmp. If there's a match, you prompt the user for a password and do the same thing with the password array, looping until they get it right.

Then, you just keep the increment counter in memory, and refer to the array for the name of the person and their bank balances. This way, you don't have to write up a huge amount of redundant code, and you just manipulate the balances by referring to their locations in the arrays...

Make sense?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

thanks, Drowzee
it makes sense all right. I'm trying to do it right now.
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

Cool.

Just to make sure I'm not confusing you:
Once you've got a valid userID, you only need to check the associated password, and loop the password prompt until it's entered correctly.

Also: Trying to get asterisks (*) entered instead of characters requires a lot more effort than it's worth to you right now.
It'd be better to just leave that kind of functionality alone.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 21st, 2005
0

Re: bank account

Quote originally posted by Drowzee ...
As I didn't see any rules against double posting in this fashion, I thought it best to split this off to help seperate the topic, as my last one was far too heavily edited and almost unusable.

Yes, your current code is far too redundant. If you ever have code that is repeated with only minor changes like the name entered, there's a better, faster way to do it.

Let me introduce you to your friend and mine, 'Switch'.
Switch is, for an integer input (like a menu choice), a very handy tool.

C++ Syntax (Toggle Plain Text)
  1.  
  2. int menu_sel;
  3.  
  4. //Get your menu selection somehow.
  5.  
  6. switch(menu_sel){
  7. case 0: //menu selection for, I dunno... Deposits.
  8. Your deposit function;
  9. case 1: //Let's say, withdrawals.
  10. your withdraw function;
  11. case 2: //for whatever else you want. Just keep adding cases.
  12. default: //This must always be at the end of the cases. It catches anything that wasn't supposed to come through. So, if someone enters G, it'll be caught here.
  13. break; //It's always a good idea to use 'break' in the default. Just do it, it's good, safe coding.
  14. } //end of the switch statement.

That'll help a little. More importantly, you'll need to make it so that you have one generic login, not five. You can do this a number of ways, but the lowest-level one would be to have two arrays of strings, . One for the names, one for the associated passwords.
Also, two arrays (or one 2-D array) of floats for the balances.

You do the log in by asking for a string, and iterate through the name array to find a match with strcmp. If there's a match, you prompt the user for a password and do the same thing with the password array, looping until they get it right.

Then, you just keep the increment counter in memory, and refer to the array for the name of the person and their bank balances. This way, you don't have to write up a huge amount of redundant code, and you just manipulate the balances by referring to their locations in the arrays...

Make sense?
Hi, i've tried the login part of your suggestion. But I don't know why I can't get get it to compile. this is my code. do you know what I did wrong?

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. //Function Prototypes
  10. string Signin (string, string);
  11. void Menu ();
  12. void ViewBalance ();
  13. void Deposit ();
  14. void Withdrawal ();
  15. void Transfer ();
  16.  
  17.  
  18. void main (void)
  19. {
  20. string Username[] = {"Ohlone1","Ohlone2","Ohlone3","Ohlone4","Ohlone5"};
  21. string PWmain[] = {"Ohlone1pw", "Ohlone2pw", "Ohlone3pw", "Ohlone4pw", "Ohlone5pw"};
  22.  
  23. cout << "WELCOME TO PROGRAMMING BANK!\n\n";
  24. cout << "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
  25.  
  26. Signin (*Username, *PWmain);
  27. Menu ();
  28. }
  29.  
  30. string Signin (string &userID, string &PWuser)
  31. {
  32. string ID, PW;
  33. cout << "Please enter your username: \n";
  34. cin.width(32);
  35. cin >> ID;
  36.  
  37. for (int i =0; i < 5; i++)
  38. {
  39. if (strcmp(ID, userID[i]) == 0)
  40. {
  41. cout << "Please enter your password: \n";
  42. cin >> PW;
  43. for (int j =0; i < 5; i++)
  44. {
  45. if (strcmp(ID, PWuser[i]) ==0)
  46. {
  47. cout << "YOU'RE LOGGED IN!\n";
  48. }
  49. }
  50. }
  51. }
  52. }
  53.  
  54. void Menu ()
  55. {
  56. int choice;
  57.  
  58. cout << "\t1. View Your Balance.\n";
  59. cout << "\t2. Deposit.\n";
  60. cout << "\t3. Withdraw.\n";
  61. cout << "\t4. Transfer.\n";
  62. cout << "\t5. Exit.\n\n";
  63. cout << "Please enter your choice: \n";
  64. cin >> choice;
  65. switch (choice)
  66. {
  67. case 1:
  68. {
  69. //cout << "Your checking account has : " << << endl;
  70. //cout << "Your saving account has : " << << endl;
  71. }
  72. case 2:
  73. {
  74. //deposit
  75. }
  76.  
  77. case 3:
  78. {
  79. //withdrawal
  80. }
  81.  
  82. case 4:
  83. {
  84. //transfer
  85. }
  86.  
  87. default:
  88. {
  89. cout << "Thank you for using this program.\n";
  90. break;
  91. }
  92. }
  93. }
  94.  
  95. void ViewBalance ()
  96. {
  97. //cout << "Your checking account has : " << << endl;
  98. //cout << "Your saving account has : " << << endl;
  99. }
  100.  
  101. void Deposit ()
  102. {
  103. int input;
  104. float deposit;
  105.  
  106. cout << "Which account would you like to deposit to?\n\n";
  107. cout << "1. Checking Account\n";
  108. cout << "2. Saving Account\n";
  109. cin >> input;
  110. if (input = 1)
  111. {
  112. cout << "How much do you want to deposit to the Checking Account?\n\n";
  113. cin >> deposit;
  114. //What to do here?
  115. }
  116. if (input =2)
  117. {
  118. cout << "How much do you want to deposit to the Saving Account?\n";
  119. cin >> deposit;
  120. //What to do here?
  121. }
  122. }
  123.  
  124. void Transfer ()
  125. {
  126. int input;
  127.  
  128. cout << "Would you like to transfer..\n\n";
  129. cout << "1. From CHECKING to SAVING\n";
  130. cout << "\nOR\n\n";
  131. cout << "2. From SAVING to CHECKING\n";
  132. cin >> input;
  133. if (input = 1)
  134. {
  135. // C_to_S ();
  136. }
  137. if (input = 2)
  138. {
  139. // S_to_C ();
  140. }
  141. }
  142.  
  143. void Withdrawal ()
  144. {
  145. int input;
  146. float withdraw;
  147.  
  148. cout << "From which account would you like to withdraw?\n\n";
  149. cout << "1. Checking\n";
  150. cout << "2. Saving\n";
  151. cin >> input;
  152. if (input = 1)
  153. {
  154. cout << "How much would you like to withdraw from the Checking account?\n\n";
  155. cin >> withdraw;
  156. //what to do here?
  157. }
  158. if (input = 2)
  159. {
  160. cout << "How much would you like to withdraw from the Saving account?\n";
  161. cin >> withdraw;
  162. //what to do here?
  163. }
  164. }


did I do the switch thing right?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading UINT32 from file?
Next Thread in C++ Forum Timeline: Email sockets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC