0

I'm working on creating an STL list of structs with different functions. Everything works except when I try to add a "friend" the computed age is just a bunch of numbers. I think the problem is either the computeAge function or the addFriend function itself, but I just can't see what's wrong with them.

*Code edited for simplicity

Program:

#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 file:

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

Output:

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
Yasmeen
  • 23
  • 4
  • 1
    One of the best programmer productivity tools out there is the debugger. With a debugger you can stroll through your program at your speed and watch what the program really does. This can really help you understand what's really going on. "Step" through the program and keep an eye out for the unexpected. The unexpected is usually a bug – user4581301 Sep 16 '20 at 02:27
  • `struct friendDat` -- You really should initialize the variables (have a default contructor) when one of these gets created. Right now, `friendDat f;` shouldn't have weird or unknown values from the start. – PaulMcKenzie Sep 16 '20 at 02:30
  • Please take some time to review [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please don't forget the *mininal* part of your [mcve]. – Some programmer dude Sep 16 '20 at 02:34
  • 1
    *"I think the problem is either the **computeAge** function or [...]"* -- you have a hypothesis. That is good. Next step: test the hypothesis. Test `computeAge` in isolation. Define several `friendDat` objects (in the code, not based on user input) and see what `computeAge` returns for each. If the wrong thing is returned, you are around 90% of the way to a [mre]. If `computeAge` checks out, you can move on to your other hypothesis. (Test it in isolation.) – JaMiT Sep 16 '20 at 02:43
  • Also, a good question would not be satisfied with a vague description like "just a bunch of numbers". That is an OK way to introduce the issue, but at some point you should give concrete expected and actual observations. (If you require input, that should also be specified, but a better idea is to not require user input in your example code.) – JaMiT Sep 16 '20 at 02:46

1 Answers1

1

I added some default variables for my struct and it worked perfectly! Thanks for all the help!

Corrected Code:

#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;

}
Yasmeen
  • 23
  • 4
  • 1
    You have other bugs in your code, that you just haven't found yet. Your [while (!infile.eof()) loop is always a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Sam Varshavchik Sep 16 '20 at 02:49
  • Rethink the block of `if`s in `computeSeason`. If the conditions of `if (bMonth == 12 || bMonth == 1 || bMonth == 2)` are met, what's the point of testing every other condition? – user4581301 Sep 16 '20 at 04:39