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 4554
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 "Programmin g 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*****@dontlo okforme.com> wrote in message
news:GV******** **********@news svr13.news.prod igy.com... 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*****@dontlo okforme.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 "Programmin g 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.c om, 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********@yah oo.com> wrote in message
news:41******** *******@yahoo.c om...
... 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*****@dontlo okforme.com> wrote in message
news:Qx******** *********@newss vr14.news.prodi gy.com... "CBFalconer " <cb********@yah oo.com> wrote in message news:41******** *******@yahoo.c om... 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*****@earthl ink.net> wrote in message
news:35******** *****@individua l.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. However no
explanation is offered other than that scanf handels end of lines very
badly. I have exeperienced such problems when doing some numerical
programming but never understood it. Things like some consequitive
scanfs would not read in values...
|
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 be that the
program will only read the first line of integers from the file, and
just print them into all the categories. Any help or suggestions
woulud be great, I swear i probably tried everything
#include <stdio.h>
|
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 problems with "%c" or is it the operating
system I'm using?
1 #include <stdio.h>
2
3 int main()
|
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
scanf cannot fill any fields it returns EOF.
I have run some tests on scanf and, so far, I've not found
an EOF return.
For example: If the programer formats scanf for an int or
|
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 the full
source. I found that every time it generate a .csv file for the error
log, instead of a txt file. What's wrong?
#include <stdio.h>
#include <time.h>
#include <string.h>
|
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*/
printf("%c\n",a);
|
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 error messages mean & what I
should do?
thanks.
|
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 2 characters");
scanf("%c%c",&a,&b);
printf("\n %c %c ",a,b);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |