我正在尝试读取包含SSN,用户名和密码列表的文件。我试图用x替换除了最后4个社交用户之外的所有社交用户,我有一个根据密码强度给出积分的系统(“累加器”用于此部分)。我无法从主要输出cout表,并希望得到一些帮助。当我尝试从main输出它时,它只是执行文件的最后一行,并且该行上的SSN不是我想要的x。感谢
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void openFile(ifstream&, string, string, string);
string xReplace(ifstream& myIn, string& social, string& username, string& password, string socialRep);
void passStrength(int& count1, int& count2, int& count3, int& accumulator, ifstream& myIn, string& social, string& username, string& password);
int main()
{
ifstream myIn;
string path, file;
string fileName;
string social;
string username;
string password;
string socialRep = "xxx-xx";
string oneLine = social + username + password;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int accumulator = 0;
openFile(myIn, path, file, fileName);
xReplace(myIn, social, username, password, socialRep);
passStrength(count1, count2, count3, accumulator, myIn, social, username, password);
myIn >> social >> username >> password >> accumulator;
while (myIn.good())
cout << "SSN\tUser Name\tPassword\tPassword Strength\n";
cout << "---------------------------------------------------------\n";
cout << social << " " << username << " " << password << " " << accumulator << "\n";
return 0;
}
void openFile(ifstream& myIn, string path, string file, string fileName)
{
path = "C:\\2430";
cout << "Enter the name of your file";
getline(cin, file);
fileName = path + "\\" + file;
cout << "The file name is " << fileName << endl;
myIn.open(fileName.c_str());
if (myIn.fail())
{
cout << "Filename was invalid\n";
exit(1);
}
cout << "The file" " " << file << "has been opened\n";
}
void passStrength(int& count1, int& count2, int& count3, int& accumulator, ifstream& myIn, string& social, string& username, string& password)
{
while (myIn >> social >> username >> password)
{
for (unsigned int i = 0; i > password.length(); i++)
if (password.length() > 8)
count1 = 1;
else count1 = 0;
int testing = -1;
testing = password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
if (testing != password.npos)
count2 = 1;
else count2 = 0;
unsigned int testing2 = -1;
testing2 = password.find_first_of("!@#$%^&*()");
if (testing2 != password.npos)
count3 = 1;
else count3 = 0;
accumulator = count1 + count2 + count3;
//cout << social << " " << username << " " << password << " " <<accumulator<< "\n";
}
}
string xReplace(ifstream& myIn, string& social, string& username, string& password, string socialRep)
{
myIn >> social >> username >> password;
social.replace(0, 6, socialRep);
return social;
}
答案 0 :(得分:0)
您需要一个容器来保存您的程序中的记录。变量可以一次保留一个数据,
您可以为以下变量social,username和password定义结构记录,如下所示:
struct record{
string social;
string username;
string password;
};
创建一个std :: vector,如下所示
std::vector<record> my_records;
因此,当您从文件中读取数据时,请按如下方式将数据插入my_record。
my_reccords.push_back(recod_data);
处理来自my_records
的记录和来自my_records
的最终打印数据。