【发布时间】:2015-04-11 23:51:46
【问题描述】:
我正在尝试读取包含 SSN、用户名和密码列表的文件。我正在尝试用 x 替换除最后 4 个社交之外的所有内容,并且我有一个根据密码强度给出分数的系统(“累加器”用于这部分)。我无法从 main 输出带有 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;
}
【问题讨论】: