hi

i have this assignment in c++, i did it, but there is one tiny error i cant figure out the solution to it. the program below is supposed to take in 3 elements value inputs from user for 3 students. so if you run it, you get input student1 birth month numerically, then birth day; then birth year. then id, age, and the gpa. and it loops for 3 students. however, it inputs the complete birthdate for each student when you are done inputting the 3rd student information a format of month/day/year for each student. i am getting 0/0/0. why?

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Date
{
private:

    int day;
    int month;
    int year;

public:

    Date() { day = 0; month = 0; year = 0; }/*  set day, month, and year to zeros */ 

    void Date::setDay(int x)
    {
        day = x;
    }
    void Date::setMonth(int x)
    {
        month = x;
    }
    void Date::setYear(int x)
    {
        year = x;
    }
    int Date::getDay()
    {
        return day;
    }
    int Date::getMonth()
    {
        return month;
    }
    int Date::getYear()
    {
        return year;
    }
};

class Student
{
private:

    Date birthdate;
    int age;
    int id;
    float gpa;

public:

    Student() { int age = 0; int id = 0; float gpa = 0; }/*  set age, id, and gpa to zeros */ 

    void Student::setAge(int x)
    {
        age = x;
    }
    void Student::setId(int x)
    {
        id = x;
    }
    void Student::setGPA(float x)
    {
        gpa = x;
    }
    void Student::setDate(Date x)
    {
        birthdate.setDay (x.getDay());
        birthdate.setMonth (x.getMonth());
        birthdate.setYear (x.getYear());
    }

    int Student::getAge()
    {
        return age;
    }
    int Student::getId()
    {
        return id;
    }
    float Student::getGPA()
    {
        return gpa;
    }
    Date Student::getDate()
    {
        return birthdate;
    }
};

void PrintStudent(Student a[], int b)
{
    float maximum_gpa = a[0].getGPA();
    float minimum_gpa = a[0].getGPA();

    int maximumAge = a[0].getAge();
    int minimumAge = a[0].getAge();

    for (int i = 0; i < b; i++)
    {
        cout << "student " << i + 1 << "'s birthdate is: " << a[i].getDate().getMonth() << "/" << a[i].getDate().getDay() << "/" << a[i].getDate().getYear() << endl;

        cout << endl;

        cout << "student " << i + 1 << "'s age is: " << a[i].getAge() << endl;

        cout << endl;

        cout << "student " << i + 1 << "'s id is: " << a[i].getId() << endl;

        cout << endl;

        cout << "student " << i + 1 << "'s gpa is: " << a[i].getGPA() << endl;

        cout << endl;

        if (a[i].getGPA() > maximum_gpa)
        {
            maximum_gpa = a[i].getGPA();
        }
        if (a[i].getGPA() < minimum_gpa)
        {
            minimum_gpa = a[i].getGPA();
        }
        if (a[i].getAge() > maximumAge)
        {
            maximumAge = a[i].getAge();
        }
        if (a[i].getAge() < minimumAge)
        {
            minimumAge = a[i].getAge();
        }
    }
    cout << "The Maximum gpa is: " << maximum_gpa << endl;

    cout << "The minimum gpa is: " << minimum_gpa << endl;

    cout << endl;

    cout << "The Maximum age is: " << maximumAge << endl;

    cout << "The minimum age is: " << minimumAge << endl;
}

int main()
{
    Student a[3];
    Date d;
    int x;
    float f;

    for (int i = 0; i < 3; i++)
    {
        cout << "Input student " << i + 1 << "'s birth month numerically: ";
        cin >> x;
        d.setMonth(x);

        cout << endl;

        cout << "Input student " << i + 1 << "'s birth day numerically: ";
        cin >> x;
        d.setDay(x);

        cout << endl;

        cout << "Input student " << i + 1 << "'s birth year: ";
        cin >> x;
        d.setYear(x);

        cout << endl;

        cout << "Input student " << i + 1 << "'s age: ";
        cin >> x;
        a[i].setAge(x);

        cout << endl;

        cout << "Input student " << i + 1 << "'s id: ";
        cin >> x;
        a[i].setId(x);

        cout << endl;

        cout << "Input student " << i + 1 << "'s gpa: ";
        cin >> f;
        a[i].setGPA(f);

        cout << endl;
    }

    PrintStudent(a, 3);

    system("pause");
    return 0;
}

i needed this in some hours from now, and its just a simple problem here (is the get day, month, year after each slash in the code wrong? thats all i think of, but i dont know how otherwise it fixes because it didnt give me errors) so any help is appreciated!

In Student class change setDate to:

void Student::setDate(Date x)
{
    this->birthdate = x;
}

Then in main() after setting the members of Date you need to call:

a[i].setDate(d);

I assume that checking for valid data wasn't a requirement. eg integer overflow, inputting a string etc

This article has been dead for over six months. Start a new discussion instead.