I am using scanf (from stdio.h) and having trouble inputting a date. I want
the user to be able to input a date in the format mm/dd/yyyy. I use the
following:
printf ("Please enter your first date in the form of mm/dd/yyyy: ");
scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year);
where date1 is a struct of type date, defined as:
struct date
{
int month;
int day;
int year;
};
Pretty simple, right? It works as expected when I input any date without
leading zeroes. However, when I input, for example, 09/07/1972, if it is
the first (of two) expected dates, it will fill the first date with zeroes,
and put the date into the second one (which is a copy of the previous two
lines for input, only for date2). If it is the second, it makes it all
zeroes and doesn't seem to register the second date. The same date,
however, works properly if I don't use any leading zeroes. Also, sometimes
it will work with one leading zero, or at least it seems to. Can anybody
enlighten me? I'm using a (junior) college textbook. Is there a different
C function I should use for input, instead? I'd like to use pure C for now,
and move on to learning C++ only once I'm rather skilled in C. Am I doing
something wrong, or is there simply something about scanf that can't handle
leading zeroes in integers? 8 4533
Eric A. Johnson wrote: scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year); where date1 is a struct of type date, defined as: struct date { int month; int day; int year; }; Pretty simple, right? It works as expected when I input any date without leading zeroes. However, when I input, for example, 09/07/1972, if it is the first (of two) expected dates, it will fill the first date with zeroes,
If you don't want interpretation of the data with base determined as 8,
10, or 15 automatically, don't use the %i specifier. %i interprets
integral data as if you had used strtol with a base of 0. You want %d
which interprets integral data as if you had used strtol with a base of
10. All this is covered in any elementary C-for-dummies book. And you
might want to check the FAQ before posting.
Eric A. Johnson wrote: I am using scanf (from stdio.h) and having trouble inputting a date. I want the user to be able to input a date in the format mm/dd/yyyy. I use the following: printf ("Please enter your first date in the form of mm/dd/yyyy: "); scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year); where date1 is a struct of type date, defined as: struct date { int month; int day; int year; }; Pretty simple, right? It works as expected when I input any date without leading zeroes. However, when I input, for example, 09/07/1972, if it is the first (of two) expected dates, it will fill the first date with zeroes, and put the date into the second one (which is a copy of the previous two lines for input, only for date2). If it is the second, it makes it all zeroes and doesn't seem to register the second date. The same date, however, works properly if I don't use any leading zeroes. Also, sometimes it will work with one leading zero, or at least it seems to. Can anybody enlighten me? I'm using a (junior) college textbook. Is there a different C function I should use for input, instead? I'd like to use pure C for now, and move on to learning C++ only once I'm rather skilled in C. Am I doing something wrong, or is there simply something about scanf that can't handle leading zeroes in integers?
Study the difference between the "%i" conversion you
are using and the "%d" that probably suits your purpose
better. Note that a leading zero is merely a place-holder
for the latter, but has other significance for the former.
If that's not enough of a hint, try to compile
int i = 09;
.... and see what the compiler says about it. (Some compilers
may say nothing much unless you make sure to invoke them in a
Standard-conforming mode and with the warnings cranked up; if
you're using gcc I recommend "-ansi -pedantic -W -Wall" on the
command line.)
--
Eric Sosman es*****@acm-dot-org.invalid
Thanks to both of you for your input. The book I'm using is "Programming in
ANSI C", Revised Edition, Copyright 1994 (although the book was used in a
class I took barely two years ago). It mentioned some of what you
mentioned, but failed to tell me all that you mentioned.
Addendum: I noticed it finally... 7 chapters after the exercise that asked
me to create the program that needs the dates inputted. This book should at
least have let me understand it earlier. Thanks again... and I'll check out
that FAQ.
"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:GV******************@newssvr13.news.prodigy.c om... I am using scanf (from stdio.h) and having trouble inputting a date. I want the user to be able to input a date in the format mm/dd/yyyy. I use the following: printf ("Please enter your first date in the form of mm/dd/yyyy: "); scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year); where date1 is a struct of type date, defined as: struct date { int month; int day; int year; }; Pretty simple, right? It works as expected when I input any date without leading zeroes. However, when I input, for example, 09/07/1972, if it is the first (of two) expected dates, it will fill the first date with zeroes, and put the date into the second one (which is a copy of the previous two lines for input, only for date2). If it is the second, it makes it all zeroes and doesn't seem to register the second date. The same date, however, works properly if I don't use any leading zeroes. Also, sometimes it will work with one leading zero, or at least it seems to. Can anybody enlighten me? I'm using a (junior) college textbook. Is there a different C function I should use for input, instead? I'd like to use pure C for now, and move on to learning C++ only once I'm rather skilled in C. Am I doing something wrong, or is there simply something about scanf that can't handle leading zeroes in integers?
"Eric A. Johnson" wrote: "Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
I am using scanf (from stdio.h) and having trouble inputting a date. I want the user to be able to input a date in the format mm/dd/yyyy. I use the following:
.... snip ... Thanks to both of you for your input. The book I'm using is "Programming in ANSI C", Revised Edition, Copyright 1994 (although the book was used in a class I took barely two years ago). It
.... snip ...
Since you got helpful answers I expect you will be back. So you
should learn that topposting is not really acceptable in technical
newsgroups such as c.l.c. Your answer belongs after, or intermixed
with, the material to which you reply, with anything not germane to
your reply snipped.
Proper bottom-posting, as described above, is acceptable
everywhere.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
... snip ... Since you got helpful answers I expect you will be back. So you should learn that topposting is not really acceptable in technical newsgroups such as c.l.c. Your answer belongs after, or intermixed with, the material to which you reply, with anything not germane to your reply snipped.
Proper bottom-posting, as described above, is acceptable everywhere.
.... snip ...
I had only posted in that fashion because I wished to thank both Martin
Ambuhl and Eric Sosman, yet I did not wish to make two replies that would,
in effect, say the same thing. Do you have any advice for when I wish to
thank numerous people for replying to my post without making multiple
replies... for example, if a dozen people helped me, each in their own
thread?
"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:Qx*****************@newssvr14.news.prodigy.co m... "CBFalconer" <cb********@yahoo.com> wrote in message news:41***************@yahoo.com... Proper bottom-posting, as described above, is acceptable everywhere. ... snip ...
I had only posted in that fashion because I wished to thank both Martin Ambuhl and Eric Sosman, yet I did not wish to make two replies that would, in effect, say the same thing. Do you have any advice for when I wish to thank numerous people for replying to my post without making multiple replies... for example, if a dozen people helped me, each in their own thread?
If more than one person answers, all the answers will be in
the *same* thread. I don't think you understand what a
Usenet thread is. It's the collection of all the messages
'attached to' and pertaining to (and including) the original
message (in this case, yours).
If you do see someone answer your question, but in some
other thread (not the one you asked in), they will be advised
to answer in the germane thread.
If you want to address several people, just post a single
message in the same thread, saying e.g. "Thanks Martin and Eric".
or "Thanks, everyone".
Finally, top-posting isn't an issue of *what* you write, but *how*.
You did correctly bottom-post this time, so I guess you understand
that part.
-Mike
Eric A. Johnson wrote: Do you have any advice for when I wish to thank numerous people for replying to my post without making multiple replies... for example, if a dozen people helped me, each in their own thread?
The best way to thank us is to stick around, asking good questions, and
eventually taking on part of the load by giving good answers to other
people's questions.
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:35*************@individual.net... Eric A. Johnson wrote:
Do you have any advice for when I wish to thank numerous people for replying to my post without making multiple replies... for example, if a dozen people helped me, each in their own thread?
The best way to thank us is to stick around, asking good questions, and eventually taking on part of the load by giving good answers to other people's questions.
Will do! Thank you, all, for your help. I hope to learn a lot, and to
eventually become a contributor in my own right. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: B Thomas |
last post by:
Hi,
I was reading O'Reilly's "Practical C programming" book and it warns
against the use of scanf, suggesting to avoid using it completely .
Instead it recomends to use using fgets and sscanf....
|
by: bas |
last post by:
hey,
I am having a lot of trouble trying to get this program to work
properly. It is supposed to read integers from a file and place them
into the categories for inventory. The problem seems to...
|
by: Peter Mount |
last post by:
Hello
I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and
ends the program. Does scanf have...
|
by: hugo27 |
last post by:
obrhy8 June 18, 2004
Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. The
..h documentation of this compiler does state that when...
|
by: mikelinyoho |
last post by:
Regards:
Where the code trouble is?
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
by: YiMkiE |
last post by:
I am trying to get the user to input the name of the .csv file they
want to check if it exist and also the name of the text file they want
to store the error if file is not exist. The following is...
|
by: main() |
last post by:
I know this is the problem that most newbies get into.
#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/...
|
by: Santosh Krisnan |
last post by:
hello all,
I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the...
|
by: shweta khare |
last post by:
basically we observed that in order to input 2 character constants at a time we
ought to give spaces....hav a peek in ter dis....
case1
void main( )
{
char a,b;
printf("\n enter...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |