Help with structure program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MonkeyHater
    New Member
    • Nov 2008
    • 13

    Help with structure program

    Hey, I am trying to write a program that allows someone to enter two separate dates and returns the later of the two. I.E. I input 2/14/2004 and 3/2/2004, so it outputs 3/2/2004. This is what I have so far:

    Code:
    #include <iostream>
    #include <istream>
    #include <iomanip>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    struct Date
    {
           int month;
           int day;
           int year;
    }array[2];
    
    int larger (Date array[]);
    
    int main()
    {
        int i;
        for (i=1; i<=2; i++)
        {
        cout <<"Please enter a month : ";
        cin.getline (array[i].month);
        cout <<"Please enter a day : ";
        cin.getline (array[i].day);
        cout <<"Please enter a year : ";
        cin.getline (array[i].year);
        }
        cout <<"The larger of the two dates is " <<larger(array) <<endl;
        system("PAUSE");
        return 0;
    }
    
    int larger(Date array[])
    {
        int l, i;
        if (a[1] > a [2])
           l=a[1];
        return l;
    }
    I keep getting errors on the getline parts and I'm not quite sure what is wrong with it. Any help is greatly appreciated.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Try reading the getline reference.

    Comment

    • MonkeyHater
      New Member
      • Nov 2008
      • 13

      #3
      I am still getting errors with the getline part of the code. I'm not sure exactly how to do it and tie it into the structure as well.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Also note that the first element of an array has index 0 (zero), not 1 (one).

        kind regards,

        Jos

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          I am still getting errors with the getline part of the code. I'm not sure exactly how to do it and tie it into the structure as well.
          As Banfa said, if you don't know what you're doing with getline, read the reference and examples you find from Google searches. If you're expecting us to give you anymore help, you need to formulate a proper question. Telling us you have problems with getline isn't a question. All we can do at the point is tell you to read the references.

          A proper question gives us details. You have to tell us precisely the problematic lines of code. What you expected to happen. ("I expected to see a black screen. I expected to see a pink pig fly. I expected to see a Ferrari popup outside my house.") Then tell us your observation ("I got <copy-pasted> errors. I saw a blue screen. I got a Ford outside my house instead of a Ferrari.") . Observations. Tell us what you did to attempt to fix the problem.

          Without information, we cannot help you.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by oler1s
            A proper question gives us details. You have to tell us precisely the problematic lines of code. What you expected to happen. ("I expected to see a black screen. I expected to see a pink pig fly. I expected to see a Ferrari popup outside my house.") Then tell us your observation ("I got <copy-pasted> errors. I saw a blue screen. I got a Ford outside my house instead of a Ferrari.") . Observations. Tell us what you did to attempt to fix the problem.
            Well, I expected to find one more package of fresh tobacco but all I saw was
            a total lack of tobacco; as a matter of fact I'm surrounded by a huge amount of
            non-tobacco. I got to run to my tobacco shop before it closes for the day ...

            kind regards,

            Jos ;-)

            Comment

            • kristien
              New Member
              • Nov 2008
              • 4

              #7
              U have to go thru the syntax of the getline function.and the index of array shud start with 0 not with 1.and one more thing u cant compare the entire array with another array jus using the > operator.

              Comment

              • lisc
                New Member
                • Apr 2008
                • 6

                #8
                int larger(Date array[])
                {
                int l, i;
                if (a[1] > a [2])
                l=a[1];
                return l;
                }

                This function has an error!
                You should compare with the year first, then the month, at last the day.

                Comment

                • MonkeyHater
                  New Member
                  • Nov 2008
                  • 13

                  #9
                  Alright I changed the code a bit and I am no longer getting errors on the getline. The current code I have is :
                  Code:
                  #include <iostream>
                  #include <istream>
                  #include <iomanip>
                  #include <string>
                  #include <cstdlib>
                  
                  using namespace std;
                  
                  struct Date
                  {
                         char month[256];
                         char day[256];
                         char year[256];
                  }; 
                  
                  int larger (Date g[]);
                  
                  int main () 
                  {
                    Date g[2];
                    int i;
                    for (i=0; i<=1; i++)
                    {
                    cout << "Enter a month: ";
                    cin.getline (g[0].month,256);
                    cout << "Enter a day: ";
                    cin.getline (g[0].day,256);
                    cout << "Enter a year: ";
                    cin.getline (g[0].year,256);
                    }
                    cout <<"The larger of the two dates is : " <<larger(g) <<endl;
                  
                    system("PAUSE");
                    return 0;
                  }
                  
                  int larger(Date a[])
                  {
                      int l;
                      if (a.year[0] > a.year[1])
                         l=a[0];
                      else if (a.year[0] < a.year[1])
                         l=a[1];
                      else if (a.month[0] > a.month[1])
                         l=a[0];
                      else if (a.month[0] < a.month[1])
                         l=a[1];
                      else if (a.day[0] > a.day[1])
                         l=a[0];
                      else if (a.day[0] < a.day[1])
                         l=a[1];
                      return l;
                  }
                  Now I am getting errors in the int larger(Date a[]) part of the program. It goes to the first loop and says " `year' has not been declared ". It does it for each loop after that and switches 'year' for 'month' and 'day' for their respected loops. I thought that the structure was declaring them and would then take the input from int main() and use it in the int larger(). At this point in time it will not even come up with the little black box for the program. Any help is appreciated.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Parameter 'a' is the array, not the elements thereof, so it should be a[0].year
                    not a.year[0] etc.

                    kind regards,

                    Jos

                    Comment

                    Working...