【问题标题】:Adding a person to an STL list of struct将人员添加到结构的 STL 列表中
【发布时间】:2021-01-02 19:47:22
【问题描述】:

我正在创建一个具有不同功能的结构的 STL 列表。一切正常,除了当我尝试添加一个“朋友”时,计算出的年龄只是一堆数字。我认为问题出在 computeAge 函数或 addFriend 函数本身,但我看不出它们有什么问题。

*为简单起见编辑代码

程序:

#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
#include<string>
#include<list>
#include<algorithm> 

struct friendDat
{
    string fName;
    string lName;
    int birthYear;
    int birthMonth;
    int birthDay;
    char sex;
    int age;
    string dayBorn;
    string season;

    int todayYear;
    int todayMonth;
    int todayDay;

    string name;
    bool operator < (const friendDat& f)
    {
        if (f.age > age)
            return true;
        else
            return false;
    }

};

typedef list<friendDat> friends;
typedef friends::iterator it_f;

void getFriends(friends& f, friendDat element);
int computeAge(friendDat element);
string computeDayBorn(friendDat element);
string computeSeason(friendDat element);

void printFriends(friends f);
void addFriend(friends& f);


int main()
{

    friendDat element;
    friends f;
    getFriends(f, element);
    addFriend(f);
    printFriends(f);


    system("pause");*/
    return 0;

}


void getFriends(friends& f, friendDat element)
{
    ifstream infile;

    cout << "Enter the numerical year(20XX), month, and day: ";
    cin >> element.todayYear >> element.todayMonth >> element.todayDay;
    cout << endl;

    string season;

    infile.open("friends.dat");

    while (!infile.eof())
    {
        infile >> element.fName >> element.lName >> element.birthYear >> element.birthMonth >> element.birthDay >> element.sex;
        element.age = computeAge(element);
        element.dayBorn = computeDayBorn(element);
        element.season = computeSeason(element);
        f.push_back(element);
    }

     f.pop_back();
    infile.close();
}

void addFriend(friends& f)
{

    friendDat element;

    cout << "Please enter the first name: " << endl;
    cin >> element.fName;
    cout << "Please enter the last name: " << endl;
    cin >> element.lName;
    cout << "Please enter the birth year: " << endl;
    cin >> element.birthYear;
    cout << "Please enter the birth month: " << endl;
    cin >> element.birthMonth;
    cout << "Please enter the birth day: " << endl;
    cin >> element.birthDay;
    cout << "Please enter the sex: " << endl;
    cin >> element.sex;
    
    element.age = computeAge(element);
    element.dayBorn = computeDayBorn(element);
    element.season = computeSeason(element);


    f.push_back(element);
    
}

void printFriends(friends f)
{
    for (it_f it = f.begin(); it != f.end(); it++)
    {
        cout << it->fName << " " << it->lName << " " << it->birthYear << " " << it->birthMonth << " "
            << it->birthDay << " " << it->sex << " " << it->age << " " << it->dayBorn << " " << it->season << endl;

    }
}



int computeAge(friendDat element)
{

    int todayYear = element.todayYear;
    int todayMonth= element.todayMonth;
    int todayDay = element.todayDay;

    int age = todayYear - element.birthYear;
    if (todayMonth < element.birthMonth)
        age--;
    if (todayMonth == element.birthMonth && todayDay < element.birthDay)
        age--;
    return age;

}

string computeDayBorn(friendDat element)
{
    int d = element.birthDay;
    int m = element.birthMonth;

    int y = element.birthYear % 100;
    int c = element.birthYear / 100;
    if (m == 1 || m == 2)
    {
        m += 12;
        y--;
    }

    int D = (d + (m + 1) * 26 / 10 + y + y / 4 + c / 4 + 5 * c) % 7;
    if (D == 0) return "Saturday";
    else if (D == 1)return "Sunday";
    else if (D == 2)return "Monday";
    else if (D == 3)return "Tuesday";
    else if (D == 4)return "Wednesday";
    else if (D == 5)return "Thursday";
    else if (D == 6)return "Friday";
    return "impossible";
}

string computeSeason(friendDat element)
{
    int bMonth = element.birthMonth;
    int bDay = element.birthDay;

    string season = "";
    if (bMonth == 12 || bMonth == 1 || bMonth == 2)
        season = "Winter";
    if (bMonth == 4 || bMonth == 5)
        season = "Spring";
    if (bMonth == 7 || bMonth == 8)
        season = "Summer";
    if (bMonth == 10 || bMonth == 11)
        season = "Fall";
    if (bMonth == 3 && bDay >= 21)
        season = "Spring";
    if (bMonth == 3 && bDay < 21)
        season = "Winter";
    if (bMonth == 6 && bDay >= 21)
        season = "Summer";
    if (bMonth == 6 && bDay < 21)
        season = "Spring";
    if (bMonth == 9 && bDay >= 21)
        season = "Fall";
    if (bMonth == 9 && bDay < 21)
        season = "Summer";

    return season;

}


friends.dat 文件:

Friend One 1998 8 23 M
Friend Two 2002 7 10 F
Friend Three 2001 5 3 M
Friend Four 2001 10 6 F
Friend Five 1999 1 10 M
Friend Six  2000 12 1 F

输出:

Enter the numerical year(20XX), month, and day: 2020 9 8

Please enter the first name:
REeeee
Please enter the last name:
ahhhh
Please enter the birth year:
1990
Please enter the birth month:
2
Please enter the birth day:
23
Please enter the sex:
F
Friend One 1998 8 23 M 22 Sunday Summer
Friend Two 2002 7 10 F 18 Wednesday Summer
Friend Three 2001 5 3 M 19 Thursday Spring
Friend Four 2001 10 6 F 18 Saturday Fall
Friend Five 1999 1 10 M 21 Sunday Winter
Friend Six 2000 12 1 F 19 Friday Winter
REeeee ahhhh 1990 2 23 F -858995451 Friday Winter

【问题讨论】:

  • 调试器是最好的程序员生产力工具之一。使用调试器,您可以以您的速度浏览您的程序并观察程序的实际运行情况。这真的可以帮助您了解真正发生的事情。 “逐步”完成程序并留意意外情况。意外通常是一个错误
  • struct friendDat -- 你真的应该在其中一个被创建时初始化变量(有一个默认的构造函数)。现在,friendDat f; 从一开始就不应该有奇怪或未知的值。
  • 请花一些时间查看the help pages,接受SO tour,阅读How to Ask,以及this question checklist。请不要忘记minimal reproducible examplemininal 部分。
  • "我认为问题出在 computeAge 函数或 [...]" -- 你有一个假设。那很好。下一步:检验假设。单独测试computeAge。定义几个friendDat 对象(在代码中,不基于用户输入)并查看computeAge 为每个对象返回什么。如果返回了错误的东西,你大约 90% 的方法是到达 minimal reproducible example。如果computeAge 签出,您可以继续进行其他假设。 (单独测试。)
  • 另外,一个好的问题不会满足于像“只是一堆数字”这样的模糊描述。这是介绍问题的好方法,但在某些时候,您应该给出具体的预期和实际观察结果。 (如果您需要输入,也应该指定,但更好的做法是在示例代码中不要求用户输入。)

标签: c++ list struct stl


【解决方案1】:

我为我的结构添加了一些默认变量,它运行良好!感谢大家的帮助!

更正的代码:

#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
#include<string>
#include<list>
#include<algorithm> 

struct friendDat
{
    string fName = "Default";
    string lName = "Friend";
    int birthYear = 2001;
    int birthMonth = 5;
    int birthDay = 5;
    char sex = 'F';
    int age = 19;
    string dayBorn = "Friday";
    string season = "Winter";

    int todayYear = 2020;
    int todayMonth = 9;
    int todayDay = 15;
    bool operator < (const friendDat& f)
    {
        if (f.age > age)
            return true;
        else
            return false;
    }

};

typedef list<friendDat> friends;
typedef friends::iterator it_f;

void getFriends(friends& f, friendDat element);
int computeAge(friendDat element);
string computeDayBorn(friendDat element);
string computeSeason(friendDat element);

void printFriends(friends f);
void addFriend(friends& f);


int main()
{

    friendDat element;
    friends f;
    getFriends(f, element);
    addFriend(f);
    printFriends(f);


    system("pause");*/
    return 0;

}


void getFriends(friends& f, friendDat element)
{
    ifstream infile;

    cout << "Enter the numerical year(20XX), month, and day: ";
    cin >> element.todayYear >> element.todayMonth >> element.todayDay;
    cout << endl;

    string season;

    infile.open("friends.dat");

    while (!infile.eof())
    {
        infile >> element.fName >> element.lName >> element.birthYear >> element.birthMonth >> element.birthDay >> element.sex;
        element.age = computeAge(element);
        element.dayBorn = computeDayBorn(element);
        element.season = computeSeason(element);
        f.push_back(element);
    }

     f.pop_back();
    infile.close();
}

void addFriend(friends& f)
{

    friendDat element;

    cout << "Please enter the first name: " << endl;
    cin >> element.fName;
    cout << "Please enter the last name: " << endl;
    cin >> element.lName;
    cout << "Please enter the birth year: " << endl;
    cin >> element.birthYear;
    cout << "Please enter the birth month: " << endl;
    cin >> element.birthMonth;
    cout << "Please enter the birth day: " << endl;
    cin >> element.birthDay;
    cout << "Please enter the sex: " << endl;
    cin >> element.sex;
    
    element.age = computeAge(element);
    element.dayBorn = computeDayBorn(element);
    element.season = computeSeason(element);


    f.push_back(element);
    
}

void printFriends(friends f)
{
    for (it_f it = f.begin(); it != f.end(); it++)
    {
        cout << it->fName << " " << it->lName << " " << it->birthYear << " " << it->birthMonth << " "
            << it->birthDay << " " << it->sex << " " << it->age << " " << it->dayBorn << " " << it->season << endl;

    }
}



int computeAge(friendDat element)
{

    int todayYear = element.todayYear;
    int todayMonth= element.todayMonth;
    int todayDay = element.todayDay;

    int age = todayYear - element.birthYear;
    if (todayMonth < element.birthMonth)
        age--;
    if (todayMonth == element.birthMonth && todayDay < element.birthDay)
        age--;
    return age;

}

string computeDayBorn(friendDat element)
{
    int d = element.birthDay;
    int m = element.birthMonth;

    int y = element.birthYear % 100;
    int c = element.birthYear / 100;
    if (m == 1 || m == 2)
    {
        m += 12;
        y--;
    }

    int D = (d + (m + 1) * 26 / 10 + y + y / 4 + c / 4 + 5 * c) % 7;
    if (D == 0) return "Saturday";
    else if (D == 1)return "Sunday";
    else if (D == 2)return "Monday";
    else if (D == 3)return "Tuesday";
    else if (D == 4)return "Wednesday";
    else if (D == 5)return "Thursday";
    else if (D == 6)return "Friday";
    return "impossible";
}

string computeSeason(friendDat element)
{
    int bMonth = element.birthMonth;
    int bDay = element.birthDay;

    string season = "";
    if (bMonth == 12 || bMonth == 1 || bMonth == 2)
        season = "Winter";
    if (bMonth == 4 || bMonth == 5)
        season = "Spring";
    if (bMonth == 7 || bMonth == 8)
        season = "Summer";
    if (bMonth == 10 || bMonth == 11)
        season = "Fall";
    if (bMonth == 3 && bDay >= 21)
        season = "Spring";
    if (bMonth == 3 && bDay < 21)
        season = "Winter";
    if (bMonth == 6 && bDay >= 21)
        season = "Summer";
    if (bMonth == 6 && bDay < 21)
        season = "Spring";
    if (bMonth == 9 && bDay >= 21)
        season = "Fall";
    if (bMonth == 9 && bDay < 21)
        season = "Summer";

    return season;

}

【讨论】:

  • 您的代码中还有其他尚未发现的错误。你的while (!infile.eof()) loop is always a bug
  • 重新思考computeSeason 中的ifs 块。如果满足if (bMonth == 12 || bMonth == 1 || bMonth == 2) 的条件,那么每隔一个条件进行测试有什么意义?
猜你喜欢
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多