char * input extraction and conversion

I am working on a class assignment requiring me to work with "time" elements as input by a user. What I want to do is get input from the user that looks like:

"HH:MM"

Where HH and MM are values in military time (00-23 and 00-59 respectively). I then want to extract the HH and MM values and store them in separate ints. For the purposes of this assignment, I am restricted to working with a dynamic array to store the input (No strings, vectors, or static arrays allowed). I am allowed the use of methods in <iostream> and <cstring>. The only thing I've come up with so far is something like:

Create two additional char arrays, one for the HH and one for the MM
Manually copy the values at input[0], input[1], input[4] and input[5] into the HH and MM arrays.
Use if statements to determine if the leading value is a 0 for each (If not, convert the two values separately, then add).
Convert using ASCII values, then store in the appropriate ints.

I can write code to do this brute force, I think, but I feel like there has to be a better solution, even within the assignment limitations. Any suggestions on algorithms or different approaches are welcome! I'm much better at data structures than IO formatting, sadly.

Last edited on
1
2
3
    int h = 0, m = 0;
    char ch;
    cin >> h >> ch >> m;

Man. I always make things harder than they have to be. Thank you!
the quick and dirty pointer code to do this (as per the assignment):

making assumption: the user puts in the data correctly; I am not checking this.

1
2
3
4
5
6
7
8
9
10
char *time = new char[10]; //dynamic array of char.
char *h =  time;  //redundant renaming 
char *m = &time[3]; //HH:MM the first m is [3] from [0] the 4th total letter. 
cin >> time;
time[2] = 0; //replace the : with end of string in C to split the strings. 
int hi, mi;
hi = atoi(h); //could have used 'time' variable here
mi = atoi(m);
cout << hi << ":" << mi << endl;
delete[] time;


the above is step by step explicit, you don't actually need the h pointer; you can use time directly instead, but I wanted to name it for you. Atoi is c-string to integer. The m pointer is also not required, but using pointer math to do that is unreadable nonsense code :) The snarky nonsense version would be

1
2
3
4
5
6
7
8
char *time = new char[10]; //dynamic array of char.
cin >> time;
time[2] = 0; //replace the : with end of string in C to split the strings. 
int hi, mi;
hi = atoi(time); //could have used 'time' variable here
mi = atoi(time+3);
cout << hi << ":" << mi << endl;
delete[] time;


Note that this is high risk (it assumes data was put in correctly and will go nuts if not) and its idiotic (see the above post on a cleaner way) but it does what the assignment asks (using a dynamic array and converting the values). The above answer is better code, but this may be more in line with what was asked, assuming you are learning hands on manipulation of strings in C or dynamic memory concepts or both.
Last edited on
Topic archived. No new replies allowed.