not to sure

This is a discussion on not to sure within the C++ Programming forums, part of the General Programming Boards category; >else cout << "th." ; I doubt you want that period there....

  1. #16
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >else cout << "th." ;
    I doubt you want that period there.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #17
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Code:
    #include <iostream.h>
    
    class date {
    
    public:
    	char WeekDay[3]; // Make this 4 characters
    	int DayOfMonth;
    	int MonthOfYear;
    	int Year;
    };
    
    void main()
    {
    date TodaysDate;
    
    // Store a date
    TodaysDate.WeekDay[0] = 'F' ;
    TodaysDate.WeekDay[1] = 'r' ;
    TodaysDate.WeekDay[2] = 'i' ;
        // The above can now be changed to:
        // strcpy(TodaysDate.WeekDay, "Fri") because you would have enough space
        // to make the value a string
    
    TodaysDate.DayOfMonth = 1 ;
    TodaysDate.MonthOfYear =5 ;
    TodaysDate.Year = 1995 ;
    
    // Now print it out again
    
    cout << "The date is: ";
    
    cout << TodaysDate.WeekDay[0]
    	 << TodaysDate.WeekDay[1]
    	 << TodaysDate.WeekDay[2] ;
        // And this can simply be:
        // cout << TodaysDate.WeekDay; 
    And this can be simplified:
    Code:
    else {
    	if (TodaysDate.DayOfMonth % 10 == 1)
    		cout << "st" ;
    	else if (TodaysDate.DayOfMonth % 10 == 2)
    		cout << "nd" ;
    	else if (TodaysDate.DayOfMonth % 10 == 3)
    		cout << "rd" ;
    	else cout << "th." ;
    }
    to
    Code:
    else {
    	int rem = TodaysDate.DayOfMonth % 10;  // calc the modulo once
    	if (rem == 1)  cout << "st" ;
    	else if (rem == 2)  cout << "nd" ;
    	else if (rem == 3)  cout << "rd" ;
    	else cout << "th." ;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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