【发布时间】:2015-11-19 10:54:08
【问题描述】:
我正在从事另一个学校项目,其中一部分我需要从 .txt 文件中获取数据并将其转换为混合类型向量......其中包含两个整数和一个字符串。我尝试了各种方法,我将其归结为 .txt 文件的输入位置,并希望将其发送到向量。当我尝试用向量做一些事情时,我的困难就出现了。我的代码段如下(我会解释一下 cmets 的用途):
bool ReadPeopleFromFile(int argc, char* argv[], vector<Person> &people) {
Person tmpPrsn;
int tmpAge;
string tmpGender;
int tmpAnualIncome;
ifstream PeopleFile("dev_people.txt"); // Try to open file
if (!PeopleFile.is_open()) {
cout << "Could not open file.\n";
return true; // indicates error
}
cout << "Starting" << endl;
while (!PeopleFile.eof()) {
PeopleFile >> tmpAge;
PeopleFile >> tmpGender;
PeopleFile >> tmpAnualIncome;
tmpPrsn.SetData(tmpAge, tmpGender, tmpAnualIncome);
tmpPrsn.Print();
people.push_back(tmpPrsn); // Need to look at this!!!
}
PeopleFile.close();
cout << "Finished reading file." << endl;
return false;
}
//I have a function that gets the user input... I took it out for this post.
vector<Person> ptntlCstmrs;
// Return people within the given age range.
vector<Person> GetPeopleWithQualifyingCharacteristics(vector<Person> ppl, int AgelowerRange, int AgeupperRange, string DesiredGender, int YIlowerRange, int YIupperRange) {
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
vector<Person> pplInRange;
int age = 0;
string gender = "";
int yearlyIncome = 0;
for (i = 0; i < ppl.size(); ++i) {
for (j = 0; j < ppl.size(); ++j) {
for (k = 0; k < ppl.size(); ++k) {
age = ppl.at(i).GetAge();
gender = ppl.at(j).GetGender();
yearlyIncome = ppl.at(k).GetYearlyIncome();
if ((age >= AgelowerRange) && (age <= AgeupperRange) && (gender == DesiredGender || gender == "Any") && (yearlyIncome >= YIlowerRange) && (yearlyIncome <= YIupperRange)) {
ptntlCstmrs.push_back(ppl.at(i));
}
}
} // I know this section is messed up... I can't figure out how to get this part to work. What I'm trying to do is take the input, and filter it based on the user-inputted criteria. Nothing that I have done has worked.
}
return pplInRange;
}
int main(int argc, char* argv[]) {
vector<Person> ptntlCstmrs;
bool hadError = false;
int ageLowerRange = 0;
int ageUpperRange = 0;
string desiredGender = "";
int yearlyIncomeLowerRange = 0;
int yearlyIncomeUpperRange = 0;
hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);
if (hadError) {
return 1; // indicates error
}
GetUserInput(ageLowerRange, ageUpperRange, desiredGender, yearlyIncomeLowerRange, yearlyIncomeUpperRange);
ptntlCstmrs = GetPeopleWithQualifyingCharacteristics(ptntlCstmrs, ageLowerRange, ageUpperRange, desiredGender, yearlyIncomeLowerRange, yearlyIncomeUpperRange);
cout << "\nNumber of potential customers = " << ptntlCstmrs.size() << endl;
}
我包含了一堆这样的东西,以防我的一些参考文献被关闭,但我不这么认为。它还告诉你我在做什么。我想帮助弄清楚如何获得对向量进行实际排序的标准,然后打印出这些向量中有多少将与用户输入的数据一起使用。 提前致谢!
【问题讨论】:
-
也许不是您的问题,但您应该阅读以下内容:stackoverflow.com/questions/5431941/…
-
我不明白您使用三重嵌套循环来获取标准。您正在获取人员
i、人员j和人员k的信息。你不应该得到人i标准,看看它是否符合标准?换句话说,i上的单个循环? -
@PaulMcKenzie 这可能是......我试图以这种方式构建它,但每次都失败了。你能告诉我如何正确地构建它吗?
-
只写一个循环并使用索引
i。它比您尝试做的更简单。 -
好的,我明白了。而我的东西还是不行。它确实更简单,感谢您指出这一点! (我有点傻)还有其他想法吗?