Question about dates

This is a discussion on Question about dates within the C++ Programming forums, part of the General Programming Boards category; What are the built-in functions to compare two dates? I am stuck again. ***EDIT*** Oops forgot to mention I am ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    Question about dates

    What are the built-in functions to compare two dates? I am stuck again.

    ***EDIT***

    Oops forgot to mention I am working a program that will calculate someones age. Using the Month, Day and Year format.


    Here is what I got so far

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    
    	string x;
    	int currentyear = 2003;
    	int dob = 'x';
    	int result= 0;
    
    	cout<<"Enter your Date of birth: ";'\n';
    		std::getline(std::cin,x);
    	cout<<x<<'\n';
    
    		result=currentyear - 'x';
    
    
    	cout<<x<<endl;
    
    
    
    	return 0;
    
    }

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Um, I'm not exactly sure about functions to compare dates for you, but there are some problems with your code:

    int dob = 'x';

    This line is assigning an integer variable the character 'x' -- if you're not getting a compile error, then it must be assigning dob the ASCII value of the character x. I don't think I understand what you're trying to do with this line, especially since you never use the variable dob in your code.

    result=currentyear - 'x';

    This needs some work, too... again, you're subtracting a character (or likely the ASCII value of the character) from the integer 2003. You also never use the variable result after this line -- did you mean to output it instead of x at the end of the program?

    If you have any specific quesions about what I said feel free to ask me!!
    My programs don't have bugs, they just develop random features.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    date problem

    with the DOB = 'x' line I was trying to get it to pull the DOB from the string that was entered by the user

    Ex.

    Enter you DOB: June 23, 1976
    ^^^
    this is what I was trying to assign 'x' to.

    Or whatevever they input for their DOB.

    With the current year - 'x' I was again trying to pull the DOB from the string that was entered by the user.

    ***EDIT***

    Ok figured how to get the year

    Code:
    #include <iostream>
    using namespace std;
    
    void main (){
    
    	int x= 2003;
    	int yob=0;
    	int result=0;
    
    	cout<<"What's your Birthday? ";
    		cin>>yob;
    
    	result = x - yob;
    
    	cout<<result<<endl;
    
    	
    }
    This will return your age in years. I need to get it to compare the days: I.E. June 23, 1976 to July 15, 2003.
    Last edited by TheShaggmeister; 07-15-2003 at 12:59 PM.

  4. #4
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Well, if the user types in "June 23, 1976", you can't just put that into an integer! Now if the user simply types a number like "23", you can use the function atoi to convert it to an integer. (That stands for ASCII to Integer, btw.)

    Try looking at STL reference websites to see if there's some date functions already provided with C++ (and I'm sure there are). I usually go to http://www.dinkumware.com/manuals/reader.aspx?lib=cpp but for some reason it is being incredibly slow today (or at least on my network) and I don't feel like searching it myself!! Also, try searching on this website, there's probably already been a post about dates.

    The easy way to compare dates would be to have the user enter in the month, day, and year separately and you could store them in three integers, then you can subtract them all like you did with the year. But if you want the user to enter a string like "June 23, 1976", that will be a bit more complicated... if you can't find any functions to get the day, month, and year from a string you'll have to parse the string yourself. Maybe some other user around here has a better suggestion...? Meanwhile, I bet you can find some useful info if you look for it hard enough.

    Sorry I wasn't able to help more!
    My programs don't have bugs, they just develop random features.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21