Link to home
Start Free TrialLog in
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?
Avatar of jkr
jkr
Flag of Germany image

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);
Avatar of Helix
Helix

ASKER

that code is too complicated for me, can you please simplify?
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.
>> that code is too complicated for me, can you please simplify?

What exactly don't you understand?
Avatar of 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
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.
>>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'?
Avatar of 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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
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();
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)
 // ...