Chaning AM to PM and vice versa

This is a discussion on Chaning AM to PM and vice versa within the C Programming forums, part of the General Programming Boards category; Hello once again, So here is the part of my code I am having a problem with: Code: if (daynight=="AM" ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    20

    Chaning AM to PM and vice versa

    Hello once again,

    So here is the part of my code I am having a problem with:

    Code:
    if (daynight=="AM" && hour == 12 && minute ==00)
               {
                  strcpy(daynight, "PM");
               }
               
               if (daynight=="PM" && hour ==12 && minute ==00)
               {
                  strcpy(daynight, "AM");
               }
    So what I essentially get is :

    11:58PM
    11:59PM
    12:00PM
    12:01PM

    when the latter two should be AM.

    Can someone please help me with this? Thank you so much

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You can't compare strings with "==". Use strcmp(), or in your case, you could hard-code the toggling of 'A' and 'P'.

    example:
    Code:
    if (hour == 12 && minute == 0) {
        if (daynight[0] == 'A') {
             daynight[0] = 'P';
        } else {
             daynight[0] = 'A';
        }
    }
    Or something like that.
    Last edited by memcpy; 04-19-2012 at 10:31 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    Thank you so very much. with your tip to use strcmp and some examples from google, I changed my code to the following and it worked great..

    Code:
               if (strcmp(daynight,"AM") && hour == 12 && minute ==00)
               {
                  strcpy(daynight, "PM");
               }
               
               if (strcmp(daynight,"P M") && hour ==12 && minute ==00)
               {
                  strcpy(daynight, "AM");
               }

    Thank you so much. You saved me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting from struct to char array and vice versa
    By Edelweiss in forum C Programming
    Replies: 6
    Last Post: 08-08-2011, 11:35 PM
  2. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  3. int to char and vice versa
    By mekaj in forum C++ Programming
    Replies: 13
    Last Post: 12-12-2005, 11:35 AM
  4. Problem: far to near copy, and vice versa
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 07:37 AM
  5. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 01:17 AM

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