Help with comparing user input to a text file!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 1
Reputation: AaronYoq is an unknown quantity at this point 
Solved Threads: 0
AaronYoq AaronYoq is offline Offline
Newbie Poster

Help with comparing user input to a text file!

 
0
  #1
Apr 4th, 2005
I'm trying to get the computer to compare a username and password entered by the user to a username and password in a text file. If a match is found, then I am going to present the user with other options. I think there must be an easier way to do this than the way I did. I will post the text file and my cpp file. Please help me figure out a better way to check for the usernames and passwords. Once I verify their information, I would like to store their information, including their address, etc, into strings and then close the file, so the file doesn't stay open the whole time.

Thank you!

First the text file--

  1. green rebecca 1001 sycamore, commerce, tx. 75428
  2. 1031
  3. 10453.56
  4.  
  5. meneir johanas 2833 robust, rowlett, tx. 77893
  6. 9873
  7. 100.45
  8.  
  9. underwood damon 283 park, greenville, sc. 99232
  10. 1113
  11. 1024.75
  12.  
  13. of-arc joan 777 lemon-street, madrid, france 21212
  14. 2323
  15. 999.54
  16.  
  17. mouse minnie 999 disney, orlando fl. 55577
  18. 9986
  19. 325.67
  20.  
  21. heath vicki 8219 westley, greenville, tx. 75404
  22. 4569
  23. 4500.00
  24.  
  25. bindlish arti 90 west beach, city, ca. 22231
  26. 9003
  27. 23.00
  28.  
  29. pitt brad 100 coolsville, city, mi. 45699
  30. 3000
  31. 130.37
  32.  
  33. khan gangis 333 uroguy, city, mongolia 34343-00000
  34. 2003
  35. 340000.99
  36.  
  37. howard debbi 4741 holiday, city, il. 44557
  38. 2347
  39. 2400.00
  40.  
  41. slausages jakey 99834 junkyard, city, tx. 55554
  42. 1032
  43. 1000.00

Here is my code:

  1. // Aaron Yoquelet ATM program
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. void balance(); //was going to implement as a function
  11. void deposit(); // Was going to be used later as a function
  12. void withdrawal(); // was going to be used later
  13. char fnuserentry(); // used as a menu for the user
  14.  
  15. int main()
  16. {
  17.  
  18. double balance1 = 0; //captures the balance
  19.  
  20. double depositamount = 0; // captures amount of deposit
  21. double withdrawalamount = 0; //withdrawal amount
  22. string uname, pwd; //strings used to compare
  23. char userentry = '1'; //user selection
  24.  
  25. do
  26. {
  27. cout << endl << "User Name : ";
  28. cin >> uname;
  29.  
  30. if(uname.length() == 0) // Makes sure that they enter a name and not nothing
  31. {
  32. cout << endl << "Please enter a name" << endl;
  33. continue;
  34. }
  35.  
  36. break;
  37.  
  38. } while(1);
  39.  
  40. do
  41. {
  42. cout << endl << "Password : ";
  43. cin >> pwd;
  44.  
  45. if(pwd.length() == 0)
  46. {
  47. cout << endl << "Password can't be empty" << endl; // So password can't be empty
  48. continue;
  49. }
  50.  
  51. break;
  52.  
  53. } while(1);
  54.  
  55. ifstream myinfile;
  56.  
  57. myinfile.open("ATM_data.txt"); //directs which file to use
  58.  
  59.  
  60. if(!myinfile) //If the data file isn't there it'll tell you to find it
  61. {
  62. cout << endl << "Please find the file ";
  63.  
  64. system("PAUSE");
  65. }
  66.  
  67. char c;
  68. string unameFromFile, pwdFromFile, fake; //fake used in clearing uname
  69. bool flag = false;
  70. int intVal;
  71. float floatVal;
  72.  
  73. do
  74. {
  75. c = myinfile.get();
  76.  
  77. if(myinfile.eof()) // end of file check
  78. break;
  79.  
  80. else if(c != ' ')
  81. unameFromFile += c;
  82.  
  83. else
  84. {
  85. do
  86. {
  87. c = myinfile.get();
  88.  
  89. if(c == '\n')
  90. break;
  91.  
  92. } while(1);
  93.  
  94. if(uname == unameFromFile) //condition to continue
  95. {
  96. myinfile >> pwdFromFile; //gets pass from file
  97.  
  98. if(pwd == pwdFromFile) //checks for a match
  99. {
  100. flag = true;
  101. system("CLS");
  102. cout << endl << "Welcome" <<endl;
  103. userentry = fnuserentry();
  104. switch( userentry )
  105. { //PLANNED TO FUNCTION CALL ALL THESE CASES
  106. case '1': myinfile.ignore(100, '\n');
  107.  
  108. myinfile >> balance1;
  109. cout << "Your balance is $ " << balance1;
  110. break;
  111. case '2': myinfile.ignore(100, '\n');
  112. myinfile >> balance1;
  113. cout << "Type in amount to deposit $ : ";
  114. cin >> depositamount;
  115. balance1 = balance1 + depositamount;
  116.  
  117. cout << "Your balance is $ " << balance1;
  118. break;
  119. case '3': myinfile.ignore(100, '\n');
  120. myinfile >> balance1;
  121. cout << "Type in amount to withdrawal $ : ";
  122. cin >> withdrawalamount;
  123. if (withdrawalamount > balance1) //Makes sure you don't overdraw
  124. {
  125. cout << "Can't do, will overdraw" <<endl;
  126. }
  127. else
  128. {
  129. balance1 = balance1 - withdrawalamount;
  130. cout << "Your balance is now $ " << balance1;
  131. }
  132. break;
  133.  
  134. default: cout << "\nYou chose to exit\n";
  135.  
  136. }
  137.  
  138. }
  139.  
  140. else // fake stores an empty string. Didn't know for sure if this was right
  141. {
  142. myinfile >> floatVal;
  143. c = myinfile.get();
  144. c = myinfile.get();
  145.  
  146. unameFromFile = fake;
  147. }
  148. }
  149.  
  150. else
  151. {
  152. myinfile >> intVal;
  153. myinfile >> floatVal;
  154. c = myinfile.get();
  155. c = myinfile.get();
  156.  
  157. unameFromFile = fake;
  158. }
  159. }
  160.  
  161. } while(1);
  162.  
  163. if(!flag)
  164. cout << endl << "Username and Password don't match";
  165.  
  166. cout << endl << endl << "Quitting" <<endl;
  167. system("PAUSE");
  168.  
  169. return(0);
  170. }
  171.  
  172. char fnuserentry() //function to provide a menu
  173.  
  174. {
  175. char userentry = 'a';
  176. cout << "Press 1 to see your balance" <<endl;
  177. cout << "Press 2 to make a deposit"<<endl;
  178. cout << "Press 3 to make a withdrawal"<<endl;
  179. cout << "Press 4 to quit"<<endl;
  180.  
  181. cin >> userentry;
  182.  
  183. return userentry;
  184.  
  185. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC