how to transfer string to character array?

I have to make a program where the user inputs a time in the standard format (hh:mm:ssPM) as a string, then I have to convert it to military time.

I have an idea of how to do it, my idea is to convert the string to a character array by taking each character of the string and turning it into a char, then if the char containing the 'P' or 'A' in PM/AM is a 'P' I would add 12 to the char containing the 'hh' part.

I know I have to use c.str() to convert but I don;t know how exactly, Do I have to create a char array of size 10? Or can I just transfer the character at position 8 of the string to see if the entered time is pm or am?

how would I convert the hh to one integer in order to add 12 to it if it's pm?
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/string/string/operator[]/

You can access the characters in the string directly without conversion. Makes your job a bit simpler.
Hi,

idknuttin wrote:
my idea is to convert the string to a character array by taking each character of the string and turning it into a char


Or, you could just accept the character array and make your code hassle free, If you find strings complex :)

just take input from user

1
2
3
4
5
char time[10];

cout<<"Input time in HH:MM:SSpm/am format :  ";

gets(time);


and continue...

hope it helps
Last edited on
@kemort
Thank you, I ended up making a program that works but it ended up being much longer than it needed to be, haha.

@programmer007
That was my first thought but I started solving problems on hackerrank so that I can get better at c++, this particular problem specifically wants to input the time as a string, I guess it's supposed to teach you how to manipulate and extract information from strings
Last edited on
Topic archived. No new replies allowed.