Determining if a string is a valid time spec

Hello,

I am relatively new to c++ and I am trying to make a program that converts time specs in the form of n1y n2n n3d n4h n5m n6s to seconds. One of the functions that I am trying to create is a function to check for if the time spec given is a valid time spec. To be valid the time spec must be in the format above (only the whitespace between the different elements can vary). For this I am trying to utilize strpbrk and search for "yndhms" in the time spec provided to the function as a string (const char*). I am trying to test my logic with the code given below. The issue that I am having is that the code does not actually output anything at the end of its runtime (either input_form is null, or I am not utilizing printf correctly). I am also having an issue trying to figure out how I would check for cases such as "abcd32y 10n 20d 10h 10m 20s" because according to my current logic that would be a valid time spec which it is not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main () {
  const char *ctime_spec = "32y 10n 20d 10h 10m 20s";
  const char *key = "yndhms";
  const char *pch;
  const char *input_form;
  pch = strpbrk ( ctime_spec, key);
  sscanf(pch, "%c", input_form);
  while (pch != NULL)
  {
    pch = strpbrk (pch+1,key);
    sscanf(pch, "%c", input_form+1);
  }
  printf ("%s", *input_form);
  return 0;
}


Thanks in advance for your help.
Since you say you're writing a C++ program why are you using C-strings instead of std::string? Why the C-stdio functions instead of C++ streams? What header files are you #including?

Where are you allocating memory for that pointer (input_form)?


Header files related to the code above are:
1
2
#include <stdio.h>
#include <string.h> 


As for the answer to the other questions this is part of an assignment and we are specified to use procedural c++ and c-strings.
Last edited on
So then this is a C program not a C++ program?

What about that last question, it's really the most important question I asked.
You also may want to look closely at lines 2 to 5 in the above code. You may want to look up what exactly that first word on each of those lines means.

Topic archived. No new replies allowed.