C String/int question + Win/Unix Compile

R

ritchie

Hello,

Just wondering if anyone could help me with a basic C string-parsing
question.

I have an Integer date ie: YYYYMMDD.
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?

Also, I am using the getch function with in a windows app.
This is just for a help function where a screen of info is displayed
and then "press any key to continue...".
It works fine but, I keep getting a warning of "'getch' undefined;
assuming extern returning int".
I compiled the same program on Solaris and got no warning for this.
Should I be including another library for this?

Thanks in advance,
Ritchie
 
J

Joona I Palaste

ritchie said:
Just wondering if anyone could help me with a basic C string-parsing
question.
I have an Integer date ie: YYYYMMDD.
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?

Yes it is. If you already have the YYYYMMDD representation in a string,
sprintf() can be used to extract the DD, MM and YYYY parts. You don't
specify how the integer value maps the date but I'm assuming it is
10000*year + 100*month + day. Then you can use the % (modulus) and /
(division) operators and sprintf() to extract the needed parts.
Also, I am using the getch function with in a windows app.
This is just for a help function where a screen of info is displayed
and then "press any key to continue...".
It works fine but, I keep getting a warning of "'getch' undefined;
assuming extern returning int".
I compiled the same program on Solaris and got no warning for this.
Should I be including another library for this?

As getch() is a non-standard function, there is no standard answer. Your
Windows or Solaris implementation might or might not supply it. Whether
it does, and if it does, then what library it is in, is completely
implementation-specific.
 
M

Mike Wahler

ritchie said:
Hello,

Just wondering if anyone could help me with a basic C string-parsing
question.

I have an Integer date ie: YYYYMMDD.

There's no such type as 'Integer' in C.
I'll assume you mean type 'int'.

I'll also assume those letters represent decimal digits,
e.g. 20040226. Note that on some platforms, this
might be representable by type 'int' but its outside
the required range of 'int' (-32767 to 32767). For
maximal portablility, use 'long int' (or better yet,
'unsigned long int')
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?

As a number:
(Using integer division):

20040226 % 100 == 26
20040226 / 100 = 200402

200402 % 100 == 2
200402 / 100 == 2004

Get the idea?


Or if you want to pick apart your string:

char date[] = "20040226"
char year[4];
char day[2];
char month[2];
memcpy(year, date, 4);
memcpy(day, date + 4, 2);
memcpy(day, date + 6, 2);

'memcpy()' is declared by said:
Also, I am using the getch function with in a windows app.

There is no such function in standard C.
This is just for a help function where a screen of info is displayed
and then "press any key to continue...".

Use the standard function 'getchar()'
It works fine but, I keep getting a warning of "'getch' undefined;
assuming extern returning int".

Because there's apparently no declaration for it in scope.
Perhaps your library doesn't have one, or you've forgotten
to #include the required header.
I compiled the same program on Solaris and got no warning for this.

C is a platform-independent language. If you stick with standard
code, the platform should not matter (assuming you have a reasonably
conforming C implementation).
Should I be including another library for this?

The standard function 'getchar()' is declared by standard
header <stdio.h>


-Mike
 
E

Eric Sosman

ritchie said:
Hello,

Just wondering if anyone could help me with a basic C string-parsing
question.

I have an Integer date ie: YYYYMMDD.
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?

Yes, by using the / and % operators.
Also, I am using the getch function with in a windows app.
This is just for a help function where a screen of info is displayed
and then "press any key to continue...".
It works fine but, I keep getting a warning of "'getch' undefined;
assuming extern returning int".
I compiled the same program on Solaris and got no warning for this.
Should I be including another library for this?

Yes: there is no getch() in the Standard C library,
so if you want to use it you must get it from somewhere
else.
 
M

Malcolm

Mike Wahler said:
There is no such function in standard C.


Use the standard function 'getchar()'
Unfortunately, on almost every implementation, characters don't become
available on stdin until the user types return. So there is no portable
solution.Sometimes non-standard functions are prototyped in standard headers. This is
an undesirable and confusing practise.
 
L

Leor Zolman

Yes it is. If you already have the YYYYMMDD representation in a string,
sprintf() can be used to extract the DD, MM and YYYY parts.

You meant sscanf(), of course.... e.g.:

#include <stdio.h>

int main(void)
{
char *date = "20040225";

int year;
int month;
int day;

if (sscanf(date, "%4d%2d%2d", &year, &month, &day) != 3)
{
printf("Conversion error.");
}
else
{
printf("year = %d, month = %d, day = %d\n",
year, month, day);
}
return 0;
}


-leor
You don't
specify how the integer value maps the date but I'm assuming it is
10000*year + 100*month + day. Then you can use the % (modulus) and /
(division) operators and sprintf() to extract the needed parts.


As getch() is a non-standard function, there is no standard answer. Your
Windows or Solaris implementation might or might not supply it. Whether
it does, and if it does, then what library it is in, is completely
implementation-specific.

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
M

Mike Wahler

Malcolm said:
Unfortunately, on almost every implementation, characters don't become
available on stdin until the user types return. So there is no portable
solution.

Change the message to "Press return to continue". :)

-Mike
 
D

Default User

ritchie said:
Hello,

Just wondering if anyone could help me with a basic C string-parsing
question.

I have an Integer date ie: YYYYMMDD.
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?

Sure. Depends on how you want to do it, but an easy way to to convert
the number first, copy the first four digits to your "year" string, next
two "month", etc.
Also, I am using the getch function with in a windows app.

Non-standard function, off-topic here.
It works fine but, I keep getting a warning of "'getch' undefined;
assuming extern returning int".
I compiled the same program on Solaris and got no warning for this.
Should I be including another library for this?

That means you are missing the header. If the library was missing, you'd
get a link error. Find out which proprietary header defines that and add
it in.



Brian Rodenborn
 
P

Peter Pichler

Mike Wahler said:
ritchie said:
Hello,

Just wondering if anyone could help me with a basic C string-parsing
question.

I have an Integer date ie: YYYYMMDD. ....
I can convert it into a string but I want to break it down to:
Int DD, int MM, int YYYY.
Is this possible?
....
Or if you want to pick apart your string:

char date[] = "20040226"
char year[4];
char day[2];
char month[2];
memcpy(year, date, 4);
memcpy(day, date + 4, 2);
memcpy(day, date + 6, 2);

'memcpy()' is declared by <string.h>

Please note that Mike's example does not include terminating nulls, hence it
does not produce valid C strings. Also, the second memcpy() should use month
as the first parameter. Try:

char date[] = "20040226"
char year[5];
char day[3];
char month[3];
strncpy(year, date, 4);
strncpy(month, date + 4, 2);
strncpy(day, date + 6, 2);
year[4] = month[2] = day[2] = '\0';

strncpy() is also declared in <string.h>

I concur with the rest of Mike's post (the parts that I snipped).
 
R

ritchie

Hi,

Thanks to all who replied.

Have it working now>
Iused the / and % operators to split the date.

Thanks again,
Ritchie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top