473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C String/int question + Win/Unix Compile

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
Nov 14 '05 #1
9 2803
ritchie <ri*********@yahoo.com> scribbled the following:
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 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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Nov 14 '05 #2

"ritchie" <ri*********@yahoo.com> wrote in message
news:3b*************************@posting.google.co m...
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 <string.h>

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
Nov 14 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message

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()'

Unfortunately, on almost every implementation, characters don't become
available on stdin until the user types return. So there is no portable
solution.
I compiled the same program on Solaris and got no warning for
this.

Sometimes non-standard functions are prototyped in standard headers. This is
an undesirable and confusing practise.
Nov 14 '05 #4
On 26 Feb 2004 20:34:28 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
ritchie <ri*********@yahoo.com> scribbled the following:
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 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.
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.


Leor Zolman
BD Software
le**@bdsoft.com
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
Nov 14 '05 #5
ritchie wrote:

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.

--
Er*********@sun.com
Nov 14 '05 #6

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c1**********@news8.svr.pol.co.uk...

"Mike Wahler" <mk******@mkwahler.net> wrote in message

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()'

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
Nov 14 '05 #7
ritchie wrote:

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
Nov 14 '05 #8
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:lR*****************@newsread2.news.pas.earthl ink.net...

"ritchie" <ri*********@yahoo.com> wrote in message
news:3b*************************@posting.google.co m...
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).
Nov 14 '05 #9
Hi,

Thanks to all who replied.

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

Thanks again,
Ritchie

Default User <fi********@boeing.com.invalid> wrote in message news:<40***************@boeing.com.invalid>...
ritchie wrote:

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

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RosalieM | last post by:
I would like to understand what python needs to work on unix. And to understand how i can make it smalest possible? I dont understand at all setup. I searched in python.org and in sources but it...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
12
by: jrefactors | last post by:
If the C programs have UNIX system calls such as fork(), alarm(), etc.., we should call it UNIX programs, not traditional C programs? We couldn't compile the programs with system calls using VC++...
18
by: Sharon | last post by:
is microsoft going to develop .Net for Unix? or at lest CLR for Unix? 10x
5
by: shan_rish | last post by:
Hi Group, When i compile the following program it compiles successfully, but crashes while executing. I am trying to assign a NULL char pointer to a string. The error message is ...
2
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've inserted a couple hundred rows in a table in Postgres via psycopg2. The first field of each row is a certain unix time (since epoch)...
9
by: collection60 | last post by:
I've been developing some Unix based shell tools. They work fine on Linux and MacOSX. I want to compile them on Win32. But I can't get hash_map to compile. I tried downloading stl (and...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.