Nov 25, 2009 at 9:37pm Nov 25, 2009 at 9:37pm UTC
im having problems with my program that needs to have the user input 2 birthdates and then determine which one is older im not to sure where to go from what i have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <math.h>
using namespace std;
void printDate (int date)
{
cout << date << endl;
}
void getDate (int day, int month, int year)
{
cout << day "/" << month "/" << year << endl;
}
int main(void )
{
int date1;
int date2;
cout << "Please enter your birthdate" << endl;
cin >> date1;
cout << "Please enter the birthdate of someone else" << endl;
cin >> date2;
cout << "Your birthday is " << getDate (date1) << endl;
return 0;
}
im having problems getting it to compile just wondering what im doing wrong and if someone can point me in the right direction
Last edited on Nov 25, 2009 at 9:37pm Nov 25, 2009 at 9:37pm UTC
Nov 25, 2009 at 9:49pm Nov 25, 2009 at 9:49pm UTC
You are missing a set of '<<' before each "/" in your getDate function.
Nov 25, 2009 at 10:00pm Nov 25, 2009 at 10:00pm UTC
thanks i didnt even realize that. i figured out that im supposed to use struct instead of void but it still doesnt work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <math.h>
using namespace std;
struct printDate {int date}
{
cout << date << endl;
}
struct getDate {int day, int month, int year}
{
cout << day << "/" << month << "/" << year << endl;
}
int main(struct )
{
int date1;
int date2;
cout << "Please enter your birthdate" << endl;
cin >> date1;
cout << "Please enter the birthdate of someone else" << endl;
cin >> date2;
cout << "Your birthday is " << getDate (date1) << endl;
cout << "Your friends birthday is " << getDate (date2) << endl;
return 0;
}
Last edited on Nov 25, 2009 at 10:01pm Nov 25, 2009 at 10:01pm UTC
Nov 27, 2009 at 7:28pm Nov 27, 2009 at 7:28pm UTC
i fixed my program a bit but now all it does is output what i input minus the first 0 how do i get it to sort between the 2 to decide which one is older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <math.h>
using namespace std;
struct printDate
{
printDate (int dd, int mm, int yyyy):
day(dd),month(mm),year(yyyy){}
int day;
int month;
int year;
};
struct getDate
{ getDate( int dd, int mm, int yyyy):
day(dd),month(mm),year(yyyy){}
}
int main()
{
int date1;
int date2;
cout << "Please enter your birthdate" << endl;
cin >> getDate >> date1;
cout << "Please enter the birthdate of someone else" << endl;
cin >> getDate >> date2;
cout << "Your birthday is " << (date1) << endl;
cout << "Your friends birthday is " << (date2) << endl;
if (date1> date2)
cout << "You are older then your friend" << endl;
else
cout << "Your friend is older then you" << endl;
return 0;
}
Last edited on Nov 27, 2009 at 8:08pm Nov 27, 2009 at 8:08pm UTC
Dec 4, 2009 at 12:04pm Dec 4, 2009 at 12:04pm UTC
Well, how do YOU compare to dates?
Compare the years. The lesser is older. If they are equal, compare the months. The lesser is older. If they are equal, compare the day. The lesser is older.
That's how the human brain does it too, we just take it for granted. You have to "tell" your computer those steps. ;)
Dec 4, 2009 at 4:55pm Dec 4, 2009 at 4:55pm UTC
Although PanGalactic is correct about ISO date format, Boost etc, I think nrose is rather trying to learn the basic concepts of C++.
As Bazzy pointed out, you are probably confusing structures, classes and functions.
What you probably want, is to define a structure that represents a date, with methods to compare and print dates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
#include <sstream>
struct Date
{
unsigned int day, month, year;
// returns: 1 if other_date is older than this date
// -1 if this date is older than other_date
// 0 if this date is equal to other_date
int compare(Date& other_date)
{
if (year > other_date.year) return 1;
if (year < other_date.year) return -1;
if (month > other_date.month) return 1;
if (month < other_date.month) return -1;
if (day > other_date.day) return 1;
if (day < other_date.day) return -1;
return 0;
}
std::string to_string()
{
std::ostringstream s;
s << day << "/" << month << "/" << year;
return s.str();
}
};
int main()
{
Date date1, date2;
std::cout << "Enter your birthdate (DD MM YYYY): " << std::endl;
std::cin >> date1.day >> date1.month >> date1.year;
std::cout << "Enter someone else's birthdate (DD MM YYYY): " << std::endl;
std::cin >> date2.day >> date2.month >> date2.year;
std::cout << "Your birthdate: " << date1.to_string() << std::endl;
std::cout << "His birthdate : " << date2.to_string() << std::endl;
std::cout << "You are " ;
switch (date1.compare(date2))
{
case -1: std::cout << "older" ; break ;
case 1: std::cout << "younger" ; break ;
case 0: std::cout << "equal" ; break ;
}
std::cout << std::endl;
}
Last edited on Dec 7, 2009 at 9:50pm Dec 7, 2009 at 9:50pm UTC
Dec 4, 2009 at 11:03pm Dec 4, 2009 at 11:03pm UTC
This is the general forum. People get general answers here. People wanting basics , well... (to usurp a phrase from Apple) "there's a forum for that."