By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,935 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,935 IT Pros & Developers. It's quick & easy.

Help with structure program

P: 13
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <istream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. struct Date
  10. {
  11.        int month;
  12.        int day;
  13.        int year;
  14. }array[2];
  15.  
  16. int larger (Date array[]);
  17.  
  18. int main()
  19. {
  20.     int i;
  21.     for (i=1; i<=2; i++)
  22.     {
  23.     cout <<"Please enter a month : ";
  24.     cin.getline (array[i].month);
  25.     cout <<"Please enter a day : ";
  26.     cin.getline (array[i].day);
  27.     cout <<"Please enter a year : ";
  28.     cin.getline (array[i].year);
  29.     }
  30.     cout <<"The larger of the two dates is " <<larger(array) <<endl;
  31.     system("PAUSE");
  32.     return 0;
  33. }
  34.  
  35. int larger(Date array[])
  36. {
  37.     int l, i;
  38.     if (a[1] > a [2])
  39.        l=a[1];
  40.     return l;
  41. }
I keep getting errors on the getline parts and I'm not quite sure what is wrong with it. Any help is greatly appreciated.
Nov 13 '08 #1
Share this Question
Share on Google+
9 Replies


Banfa
Expert Mod 5K+
P: 8,916
Try reading the getline reference.
Nov 14 '08 #2

P: 13
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.
Nov 14 '08 #3

Expert 10K+
P: 11,448
Also note that the first element of an array has index 0 (zero), not 1 (one).

kind regards,

Jos
Nov 14 '08 #4

Expert 100+
P: 671
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.
Nov 14 '08 #5

Expert 10K+
P: 11,448
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 ;-)
Nov 14 '08 #6

P: 4
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.
Nov 15 '08 #7

P: 6
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.
Nov 15 '08 #8

P: 13
Alright I changed the code a bit and I am no longer getting errors on the getline. The current code I have is :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <istream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. struct Date
  10. {
  11.        char month[256];
  12.        char day[256];
  13.        char year[256];
  14. }; 
  15.  
  16. int larger (Date g[]);
  17.  
  18. int main () 
  19. {
  20.   Date g[2];
  21.   int i;
  22.   for (i=0; i<=1; i++)
  23.   {
  24.   cout << "Enter a month: ";
  25.   cin.getline (g[0].month,256);
  26.   cout << "Enter a day: ";
  27.   cin.getline (g[0].day,256);
  28.   cout << "Enter a year: ";
  29.   cin.getline (g[0].year,256);
  30.   }
  31.   cout <<"The larger of the two dates is : " <<larger(g) <<endl;
  32.  
  33.   system("PAUSE");
  34.   return 0;
  35. }
  36.  
  37. int larger(Date a[])
  38. {
  39.     int l;
  40.     if (a.year[0] > a.year[1])
  41.        l=a[0];
  42.     else if (a.year[0] < a.year[1])
  43.        l=a[1];
  44.     else if (a.month[0] > a.month[1])
  45.        l=a[0];
  46.     else if (a.month[0] < a.month[1])
  47.        l=a[1];
  48.     else if (a.day[0] > a.day[1])
  49.        l=a[0];
  50.     else if (a.day[0] < a.day[1])
  51.        l=a[1];
  52.     return l;
  53. }
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.
Nov 15 '08 #9

Expert 10K+
P: 11,448
Parameter 'a' is the array, not the elements thereof, so it should be a[0].year
not a.year[0] etc.

kind regards,

Jos
Nov 15 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.