-
Notifications
You must be signed in to change notification settings - Fork 0
/
Credit inquiry program.cpp
75 lines (68 loc) · 1.88 KB
/
Credit inquiry program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Function to check if a given credit card number is valid
bool isValidCCNum(string ccNum) {
int sum = 0;
bool alternate = false;
for (int i = ccNum.length() - 1; i >= 0; i--) {
int digit = ccNum[i] - '0';
if (alternate) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
alternate = !alternate;
}
return (sum % 10 == 0);
}
// Function to print a credit report for a given user
void printCreditReport(string username) {
// Open file for reading
ifstream inputFile("credit.txt");
if (!inputFile.is_open()) {
cout << "Error opening file." << endl;
return;
}
// Search for user in file
string line;
bool userFound = false;
while (getline(inputFile, line)) {
if (line == username) {
userFound = true;
break;
}
}
// Print credit report for user if found
if (userFound) {
cout << "Credit report for " << username << ":" << endl;
getline(inputFile, line); // skip username line
cout << "Credit score: " << line << endl;
getline(inputFile, line); // skip credit score line
cout << "Credit history: " << endl;
while (getline(inputFile, line)) {
cout << line << endl;
}
} else {
cout << "User not found." << endl;
}
inputFile.close();
}
int main() {
string ccNum;
cout << "Enter your credit card number: ";
cin >> ccNum;
if (isValidCCNum(ccNum)) {
cout << "Credit card number is valid." << endl;
string username;
cout << "Enter your username: ";
cin >> username;
printCreditReport(username);
} else {
cout << "Invalid credit card number." << endl;
}
return 0;
}