Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. std::string getCardString() {
  13.     std::string userInput{""};
  14.  
  15.     std::cout << "Please enter a card number :: " << std::endl;
  16.     std::getline(std::cin, userInput);
  17.    
  18.     return userInput;
  19. }
  20.  
  21. std::string removeNondigitCharacters(std::string &userInputUnfiltered) {
  22.     std::string filteredInput{""};
  23.    
  24.     for(char &c : userInputUnfiltered) {
  25.         if(isdigit(c))
  26.             filteredInput += c;
  27.     }
  28.    
  29.     return filteredInput;
  30. }
  31.  
  32. bool luhnCheck(std::string userInput) {
  33.     int sum{0};
  34.     int currentNumber{0};
  35.    
  36.     for(int i = 0; i < userInput.length(); i++) {
  37.         currentNumber = userInput[i]-48;
  38.        
  39.         if(i==0 || i%2==0) {
  40.             if(currentNumber*2>=10)
  41.                 sum+=currentNumber/10+currentNumber%10;
  42.             else
  43.                 sum+=currentNumber;
  44.         } else
  45.             sum+=currentNumber;
  46.        
  47.     }
  48.    
  49.     if(sum%10!=0)
  50.         return true;
  51.     return false;
  52. }
  53.  
  54. void appendToFile(std::string &userInput, std::string fileName) {
  55.     //creating a stream to open files with
  56.     std::ofstream fileToAppend;
  57.     //opening a file and creating one if there isnt one
  58.     fileToAppend.open(fileName, std::ios_base::app);
  59.     //appending the users card to the file
  60.     fileToAppend << userInput << "\n";
  61.     //closing the file to prevent data leaks
  62.     fileToAppend.close();
  63.  
  64. }
  65.  
  66.  
  67.  
  68. int main () {
  69.     bool anotherCard = true;
  70.    
  71.     while(anotherCard) {
  72.         std::string answer{""};
  73.         std::string userInput{""};
  74.        
  75.         userInput = getCardString();
  76.        
  77.         std::cout << "Pre-Sanitized :: " << userInput << std::endl;
  78.        
  79.         userInput = removeNondigitCharacters(userInput);
  80.         std::cout << "Sanitized :: " << userInput << std::endl;
  81.        
  82.         if(luhnCheck(userInput)) {
  83.             std::cout << "VALID - " << userInput << std::endl;
  84.             appendToFile(userInput, "valid_cards.txt");
  85.         } else {
  86.             std::cout << "INVALID - " << userInput << std::endl;
  87.             appendToFile(userInput, "invalid_cards.txt");
  88.         }
  89.        
  90.         std::cout << "Would you like to do another card? (y/n)" << std::endl;
  91.         std::getline(std::cin, answer);
  92.         if(answer == "y" || answer == "Y")
  93.             anotherCard = true;
  94.         else
  95.             anotherCard = false;
  96.     }
  97.    
  98.    
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement