我正在研究另一个学校项目,其中一部分我需要从.txt文件中获取数据并将其转换为混合类型向量...其中包含两个整数和一个字符串。我已经尝试了各种方法,我把它放到输入.txt文件的地方,并希望发送到矢量。当我尝试用向量做事时,我遇到了困难。我的代码段如下(我将解释注释的内容):
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;
}
我包含了一些这样的东西,以防我的一些参考资料被关闭,但我不这么认为。它还告诉你我在做什么。我想帮助弄清楚如何获得实际排序向量的标准,然后打印出这些向量中有多少将用于用户输入的数据。 提前谢谢!
答案 0 :(得分:1)
归功于@PaulMcKenzie。上述代码的问题在以下部分中:
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;
需要更改为以下内容:
vector<Person> GetPeopleWithQualifyingCharacteristics(vector<Person> ppl, int Agelow, int Ageup, string DesGen, int YIlow, int YIup) {
unsigned int i = 0;
int age = 0;
string gender = "";
int yearlyIncome = 0;
ifstream PeopleFile("dev_people.txt");
while (PeopleFile >> age >> gender >> yearlyIncome);
vector<Person> pplInRange;
for (i = 0; i < ppl.size(); ++i) {
age = ppl.at(i).GetAge();
gender = ppl.at(i).GetGender();
yearlyIncome = ppl.at(i).GetYearlyIncome();
if ((age >= Agelow) && (age <= Ageup) && (DesGen == gender || DesGen == "Any") && (yearlyIncome >= YIlow) && (yearlyIncome <= YIup)) {
pplInRange.push_back(ppl.at(i));
}
}
return pplInRange;
}
现在它运作正常。谢谢保罗!
答案 1 :(得分:0)
我最初的想法是你没有正确收集用户输入。您将用于确定Person
对象是否为潜在客户的变量发送到另一个函数中。
默认情况下,C ++通过值传递参数 - 这意味着当参数传递给函数时,会在整个函数中生成并使用所述参数的副本。当函数结束时,编辑的参数将被丢弃,并且所有更改/编辑都将丢失。如果您想从其他方法获取用户输入并保存所述输入,请确保通过参考传递参数。
通过引用传递实质上告诉机器该特定参数在内存中的位置,并允许程序直接访问该内存,而不是在执行函数时复制参数。这反过来对参数进行了任何更改!
希望这有帮助!