Avatar of Helix
Helix
 asked on

Structures and programs

Im just having a fiddle with some code from a book of mine,

Basically There is a structure like:

struct date
      {      int day;
      char string[8];
            int year;
      };

The book says the user has to input 2 birthdays and a function, called check has to see if they're the same.

Now have had a go with doing something like:

struct date birthdayone

cin << birthday1.day << birthday1.string  etc

and the same with birthdaytwo.

Im just having problems trying to put all my inputed data for birthday1 and birthday2 into a function that checks to see if they're equal. I could put all six inputs but that seems sloppy

opinions anyone?
C++

Avatar of undefined
Last Comment
AJ_Lenny

8/22/2022 - Mon
jkr

1st of all,

>> char string[8];

will cause trouble, since 'string' also is an STL class - I'd recommend using

struct date
{
    int day;
    char month[8];
    int year;
};


As for your Q, I'd use helper functions, e.g.

void ReadDate ( istream& str, date& r) {

    str >> r.day >> r.month>> r.year;
}

bool IsSameDate ( date& r1, date& r2) {

    if ( r1.day != r2.day) return false;
    if ( r1.year != r2.year) return false;

    if ( strcmp(r1.month,r2.month)) return false;

    return true;
}


//.....

date d1;
date d2;

ReadDate ( cin, d1);
ReadDate ( cin, d2);

bool bTheSame = IsSameDate(d1,d2);
Helix

ASKER
that code is too complicated for me, can you please simplify?
harsha_dp

I think it is not a big task.. Just comparision of Structure members. Previous comment has the answer that u expected. If u r intersted you can understand some more methodologies to deal with date.
Your help has saved me hundreds of hours of internet surfing.
fblack61
jkr

>> that code is too complicated for me, can you please simplify?

What exactly don't you understand?
Helix

ASKER
wel for int main ive just used a handful of cin statements, i cant see how i can implement that code above without having to re-write the whole thing
harsha_dp

ok..

As u know u can compare structure members directly..

int isSameDate(date d1, date d2)
{
   if(d1.day != d2.day || d1.year!=d2.year || strcmpi(d1.month,d2.month)!=0)
     return 0;
   return 1;
}

Better you need to declare string as month , as it can spawn the object of string class if u declare as string.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jkr

>>i cant see how i can implement that code above without having to re-write the whole thing

When you read in a struct, you do that by

date d1;

ReadDate ( cin, d1);

for each structure. BTW, what's so wrong about rewriting the code it it is 'just from a book'?
Helix

ASKER
iive tried the isSameDate function but get:

C:\Documents and Settings\Dave\My Documents\Random\2.cpp(8) : error C2143: syntax error : missing ')' before '.'
C:\Documents and Settings\Dave\My Documents\Random\2.cpp(8) : error C2143: syntax error : missing ';' before '.'
C:\Documents and Settings\Dave\My Documents\Random\2.cpp(8) : error C2059: syntax error : ')'
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
itsmeandnobodyelse

>> C:\Documents and Settings\Dave\My Documents\Random\2.cpp(8) : error C2059: syntax error : ')'

You have to change member variable string ---> month - as jkr told you in a previous comment

struct date
{
    int day;
    char month[8];   //  don't use name 'string' because of STL class std::string
    int year;
};

Otherwise, IsSameDate would not compile because of unknown "r1.month"
--------------------------------------------------------------------------------------

I would suggest the following compare function:

#include <iostream>
#include <string>
using namespace std;

    bool operator==(const date& dt1, const date& dt2)
    {
        return dt1.day   == dt2.day &&
               string(dt1.month) == string(dt2.month) &&
               dt1.year  == dt2.year;
    }

The read function can be enhanced to:

    date readDate ( )
    {
        date  dt = { 0 };  // init with binary zeros
        while (dt.day <= 0 || dt.day >= 31 )
        {
            cout << "Enter day (1-31) ";
            cin >> dt.day;
        }      
   
        while (dt.month[0] == '\0' )
        {
            string input;
            string validMonths = "JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC/";
            cout << "Enter month (" << validMonths  << ") : ";
            cin >> input;
            for (int i = 0; i < input.length(); ++i)
                input[i] = toupper(input[i]);

            if (input.length() == 3 && validMonths.find(input + '/') != string::npos)
                 strcpy(dt.month, input.c_str());
        }
       
        while (dt.year < 1860 || dt.year > 2005 )
        {
            cout << "Enter year (YYYY) ";
            cin >> dt.year;
        }      
 
        return dt;
    }

------------------------------------------------
You may use the functions like that:

   int main()
   {
          bool goon = true;

          while (goon)
          {
              cout << endl << "Enter First Birthdate" << endl << endl;;
              date birthday1 = ReadDate();
              cout << endl << "Enter Second Birthdate" << endl << endl;;
              date birthday2 = ReadDate();
     
              if (  birthday1 == birthday2 )
                  cout <<  endl << "The given dates are equal" << endl;
              else
                  cout <<  endl << "The given dates are different" << endl;
              char quit;
              cout <<  endl << "Enter 'q' to quit or 'c' to continue: ";
              cin >> quit;
              if (quit == 'q' || quit == 'Q')
                   break;
           }
           return 0;
   }

Regards, Alex
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
itsmeandnobodyelse

Correction: (I changed the ReadDate function to readDate)

             cout << endl << "Enter First Birthdate" << endl << endl;;
              date birthday1 = readDate();
              cout << endl << "Enter Second Birthdate" << endl << endl;;
              date birthday2 = readDate();
AJ_Lenny

The easiest way is to use that code:

struct date
{
      int day;
    char string[8];
    int year;
   
    // Operator that checks whether date's are equal or not
    bool operator==(const date& d)
    {
       if (day == d.day && year == d.year && strcmp(string, d.string) == 0)
          return true;
       return false;
    }
};

And you can use it just like that:

date d1, d2;
// setting struct fields
if (d1 == d2)
 // ...