Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <iostream>
- #include <fstream>
- std::string getCardString() {
- std::string userInput{""};
- std::cout << "Please enter a card number :: " << std::endl;
- std::getline(std::cin, userInput);
- return userInput;
- }
- std::string removeNondigitCharacters(std::string &userInputUnfiltered) {
- std::string filteredInput{""};
- for(char &c : userInputUnfiltered) {
- if(isdigit(c))
- filteredInput += c;
- }
- return filteredInput;
- }
- bool luhnCheck(std::string userInput) {
- int sum{0};
- int currentNumber{0};
- for(int i = 0; i < userInput.length(); i++) {
- currentNumber = userInput[i]-48;
- if(i==0 || i%2==0) {
- if(currentNumber*2>=10)
- sum+=currentNumber/10+currentNumber%10;
- else
- sum+=currentNumber;
- } else
- sum+=currentNumber;
- }
- if(sum%10!=0)
- return true;
- return false;
- }
- void appendToFile(std::string &userInput, std::string fileName) {
- //creating a stream to open files with
- std::ofstream fileToAppend;
- //opening a file and creating one if there isnt one
- fileToAppend.open(fileName, std::ios_base::app);
- //appending the users card to the file
- fileToAppend << userInput << "\n";
- //closing the file to prevent data leaks
- fileToAppend.close();
- }
- int main () {
- bool anotherCard = true;
- while(anotherCard) {
- std::string answer{""};
- std::string userInput{""};
- userInput = getCardString();
- std::cout << "Pre-Sanitized :: " << userInput << std::endl;
- userInput = removeNondigitCharacters(userInput);
- std::cout << "Sanitized :: " << userInput << std::endl;
- if(luhnCheck(userInput)) {
- std::cout << "VALID - " << userInput << std::endl;
- appendToFile(userInput, "valid_cards.txt");
- } else {
- std::cout << "INVALID - " << userInput << std::endl;
- appendToFile(userInput, "invalid_cards.txt");
- }
- std::cout << "Would you like to do another card? (y/n)" << std::endl;
- std::getline(std::cin, answer);
- if(answer == "y" || answer == "Y")
- anotherCard = true;
- else
- anotherCard = false;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement