compound if conditions not working as I think they ought

What am I doing wrong in this brief sketch. I expect the hh integer to end with a value of 15 and instead it remains 3?
Also, is there a better way to convert a 12hr format to 24hr format? Note this is just the problem piece of code.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);        // allow for displaying values
  int hh, i = 0;             // following code is to convert hours to 24 hr format from 12 hour format
  char vp[25] = "03:15:27 PM", hrStg[3], ampm[2];
  hrStg[0] = vp[i + 0]; hrStg[1] = vp[i + 1]; hrStg[2] = '\0'; hh = atoi(hrStg);
  ampm[0] = vp[i + 9];  ampm[1]  = '\0';
  if ((ampm == "P" ) && (hh != 12)) {
    hh = hh + 12;
    itoa(hh, hrStg, 10);
  }
  Serial.println(vp);
  Serial.println(hh);      // why is this showing 3 when I think it should be 15
  Serial.println(hrStg);
  Serial.println(ampm);
  Serial.println(i);
}

Welcome to the Forum. Please read this post:

How to use this forum - please read.

Please post your code using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpreted by the forum code as italics or funny emoticons.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

Also please put only one statement on each line. It is difficult to read otherwise.

thanks for heads up. I have embedded code and removed attachment.

  if ((ampm == "P" ) && (hh != 12)) {

You can not compare strings with ==.

'strcmp'

You don't need to compare the "M" in "PM" or "AM" every time. It's always the same. Just use == to look for 'P'.

I expect the hh integer to end with a value of 15 and instead it remains 3?

Your expectations are wrong. The address of ampm is NOT equal to the string literal "P", so the statement is false.

So I changed if ((ampm == "P" ) && (hh != 12))
to

if ((strcmp(ampm,"P")) && (hh != 12))

with no difference in result. What am I still missing?

What am I still missing?

An understanding of what strcmp() does. A 0 return code, false, means the strings match. A + value means that the first string comes before the second string in the dictionary. A - value means that the second string comes first.

  1. OK, PaulS. I see what you saying - more clearly than the references I looked at. Thank you. I made it a !strcmp() and that gave me the answer I was looking for.

  2. aarg #5. I have it in the setup() so that I only do this once.

  3. a general comment: the way C seems to handle character comparisons seems to me to be very counter-intuitive. There should be easy, intuitive character handling and comparing functions - which seem to not exist. Am I missing something?

Am I missing something?

Reasonable expectations and/or years of experience.

jencliff:
2. aarg #5. I have it in the setup() so that I only do this once.

You seem to have entirely missed my point. The only difference between "AM" and "PM" is one letter, which you can compare with == like this:

if ((ampm[0] == 'P' ) && (hh != 12)) {
  1. PaulS #10 - yep, started programming in 1963.

  2. aarg #11 - I was only comparing for the "P" I set the variable ampm up as follows:
    ampm[0] = vp[i + 9]; ampm[1] = '\0';
    When I try to compile your if statement ** if ((ampm[0] == 'P' ) && (hh != 12)) {**
    i get the following:
    ISO C++ forbids comparison between pointer and integer [-fpermissive]
    possibly I didn't copy it correctly or we are using different compilers - I am using Arduino 1.6.7