Advertisement
Guest User

working example

a guest
Jul 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5.  
  6.  
  7. int main()
  8. {
  9. // Loop for ever, so user can enter pins to search for them in a file
  10. while(true)
  11. {
  12. // User Input
  13. std::cout << ("Please enter your PIN: ");
  14. std::string pin;
  15. std::cin >> pin;
  16.  
  17. // Validate user input
  18. // if this fails, the loop will continue to the start!!! *IMPORTANT*
  19. if(pin.length() != 4) {
  20. std::cout << "\033[2J\033[1;1H";
  21. std::cout << "Your pin is to long/short/incorrect. Please try again\n";
  22. std::this_thread::sleep_for(std::chrono::seconds(3));
  23. continue;
  24. }
  25.  
  26. // If we got here, the above verifcation was successful!! *IMPORANT*
  27. // Open the file to begin a search for the pin entered above **IMPORATNT**
  28. std::ifstream file_in("file.txt");
  29.  
  30. // Search file for pin **STARTING AT BEGINNING**
  31. while (file_in)
  32. {
  33.  
  34. // Read next line from file
  35. std::string line;
  36. std::getline(file_in, line);
  37.  
  38. // Check if the above getline failed if we have read the entire file
  39. // When we have completed reading the whole file, the eof() END_OF_FILE
  40. // flag is set. So we cant read any more data. EXIT THE LOOP - BREAK.
  41. if(file_in.eof()) { break; }
  42.  
  43.  
  44. // if we got here, the above getline DIDNT fail. so there is data in the
  45. // line string. Print it.
  46.  
  47. // Print the line
  48. std::cout << line << "\n";
  49. }
  50.  
  51. // Close file after search is concluded!!!! **IMPORATNT**
  52. file_in.close();
  53. }
  54.  
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement