Thread: not to sure

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    Question not to sure

    Hello all,

    I’m a newby practicing C++. I’ve copy this code from a book am reading.

    I’m not sure what the *if else* statements and the *switch* statements are used here for.

    I also don’t understand what values the modulus (%) operations in the *if* statements are supposed to return.
    Here it goes:

    Code:
    #include <iostream.h>
    
    class date {
    
    public:
    	char WeekDay[3];
    	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' ;
    TodaysDate.DayOfMonth = 17 ;
    TodaysDate.MonthOfYear = 11 ;
    TodaysDate.Year = 1995 ;
    
    // Now print it out again
    
    cout << "The date is: ";
    
    cout << TodaysDate.WeekDay[0]
    	 << TodaysDate.WeekDay[1]
    	 << TodaysDate.WeekDay[2] ;
    
    cout << " The " << TodaysDate.DayOfMonth ;
    
    if ((TodaysDate.DayOfMonth = 11) ||
    	(TodaysDate.DayOfMonth = 12) ||
    	(TodaysDate.DayOfMonth = 13)) cout << "th" ;
    else
    if (TodaysDate.DayOfMonth % 10 == 1)
    		cout << "st" ;
    if (TodaysDate.DayOfMonth % 10 == 2)
    		cout << "nd" ;
    if (TodaysDate.DayOfMonth % 10 == 3)
    		cout << "rd" ;
    		else cout << "th." ;
    
    cout << " of " ;
    
    switch (TodaysDate.MonthOfYear)
    {
    	case 1 : cout << "January"   ; break ;
    	case 2 : cout << "February"  ; break ;
    	case 3 : cout << "March"     ; break ;
    	case 4 : cout << "April"     ; break ;
    	case 5 : cout << "May"       ; break ;
    	case 6 : cout << "June"      ; break ;
    	case 7 : cout << "July"      ; break ;
    	case 8 : cout << "August"    ; break ;
    	case 9 : cout << "September" ; break ;
    	case 10 : cout <<"October"   ; break ;
    	case 11 : cout <<"November"  ; break ;
    	case 12 : cout <<"December"  ;
    }
    
    cout << ", " << TodaysDate.Year ;
    
    }
    If somebody has any idea Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    the mod operator returns the remainder of a division if im not mistaken.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I’m not sure what the *if else* statements and the *switch* statements are used here for.
    The if statements are used to determine which suffix to add to the day of the month (1st, 2nd, 3rd, 4th) by using the modulus operator to return the remainder of a division. The switch simply chooses which month name to print based on a month number.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Yes... but what I dont understand is how.

    *TodaysDate.DayOfMonth* is 17. Right?
    So... 17 % 10 == 1 = what?

    Also the switch statements dont do anything.

    strange.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So... 17 % 10 == 1 = what?
    17 % 10 is 7, so the suffix would be "th". Pull out a calculator and try this things out on different values, see if they work.

    >Also the switch statements dont do anything.
    It does for me. The switch statement prints "November" with the fixed code from your post.

    You also have an assignment bug in the first if statement:
    Code:
    if ((TodaysDate.DayOfMonth = 11) ||
    	(TodaysDate.DayOfMonth = 12) ||
    	(TodaysDate.DayOfMonth = 13)) cout << "th" ;
    All of those = should be ==. And your bracing appears to be off in the other if statements.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    it prints "th" 2x too
    When no one helps you out. Call google();

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it prints "th" 2x too
    That's because (as I said) the bracing is off. It should be:
    Code:
      if ((TodaysDate.DayOfMonth == 11) ||
        (TodaysDate.DayOfMonth == 12) ||
        (TodaysDate.DayOfMonth == 13)) cout << "th" ;
      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." ;
      }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Whoops. Youre right... the switch does the job.

    I changed the value of *TodaysDate.DayOfMonth * to 1 or 2 or 3 but it still gives me *th*.
    How do I get it to print st or nd or rd.

    Thanks

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do I get it to print st or nd or rd.
    See my last post concerning a fix for the entire if sequence.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Nah... It still returns "th".

    >>So... 17 % 10 == 1 = what?

    >17 % 10 is 7, so the suffix would be "th".

    Right.

    But it supposed to return "st"
    cout << "st" ;

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Let's see your updated code then.
    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

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Code:
    #include <iostream.h>
    
    class date {
    
    public:
    	char WeekDay[3];
    	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' ;
    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] ;
    
    cout << " The " << TodaysDate.DayOfMonth ;
    
    if ((TodaysDate.DayOfMonth = 11) ||
    	(TodaysDate.DayOfMonth = 12) ||
    	(TodaysDate.DayOfMonth = 13)) cout << "th" ;
    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." ;
    }
    
    cout << " of " ;
    
    switch (TodaysDate.MonthOfYear)
    {
    	case 1 : cout << "January"   ; break ;
    	case 2 : cout << "February"  ; break ;
    	case 3 : cout << "March"     ; break ;
    	case 4 : cout << "April"     ; break ;
    	case 5 : cout << "May"       ; break ;
    	case 6 : cout << "June"      ; break ;
    	case 7 : cout << "July"      ; break ;
    	case 8 : cout << "August"    ; break ;
    	case 9 : cout << "September" ; break ;
    	case 10 : cout <<"October"   ; break ;
    	case 11 : cout <<"November"  ; break ;
    	case 12 : cout <<"December"  ;
    }
    
    cout << ", " << TodaysDate.Year ;
    
    }

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Go to the Build menu, and click "Rebuild All". That might help. If not, try putting the cout << "th" on the next line instead of right after the else. Maybe your compiler's being stupid. I don't see anything immediately wrong with the code though, other than 'void main', which (hopefully) won't be discussed to any great extent in this thread.

    **EDIT** If DayOfMonth is 17, it's supposed to say 'th'. But, if you've changed that to 1 (as in, the '1' isn't just a typo), I don't see why it shouldn't say 'st'.
    Last edited by Hunter2; 04-21-2004 at 08:51 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Do you get 'st' on your compiler?

    Im using VisualC++... and it doesnt want to know

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh, you didn't fix what Prelude pointed out.

    Code:
    if ((TodaysDate.DayOfMonth = 11) ||
    	(TodaysDate.DayOfMonth = 12) ||
    	(TodaysDate.DayOfMonth = 13)) cout << "th" ;
    '=' is the assignment operator, not the equality operator. Of course it's outputting "th" - that if statement is always true. Replace the '=' with '=='.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed