my code:
do
{
printf("please input the dividend and the divisor.\n");
if(!scanf("%d%d ",&dend,&do r))
{
temp1=1;
fflush(stdin);
}
else
temp1=0;
}while(temp1==1 );
it seems that it only depend on the first number it read.
if I input " a 32 ", it could know there is a error,
but if I input " 32 a ",
it accept that.
thanks in advance. 51 3939
moosdau wrote: my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d ",&dend,&do r)) { temp1=1; fflush(stdin); } else temp1=0; }while(temp1==1 );
it seems that it only depend on the first number it read. if I input " a 32 ", it could know there is a error, but if I input " 32 a ", it accept that.
thanks in advance.
Hi,
In "man scanf", I suggest you to read chapter "return values".
Kind regards.
do you mean the linux os?
but I'm using windows,so I can't use the "man" command.
could you tell me plz?
moosdau wrote: do you mean the linux os? but I'm using windows,so I can't use the "man" command. could you tell me plz?
For windows, look up the function at http://msdn.microsoft.com/
(A little hint is that scanf returns the nr of successfully
assigned items)
"moosdau" <mo*****@gmail. com> writes: my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d ",&dend,&do r)) { temp1=1; fflush(stdin); } else temp1=0; }while(temp1==1 );
it seems that it only depend on the first number it read. if I input " a 32 ", it could know there is a error, but if I input " 32 a ", it accept that.
scanf() returns the number of items assigned. If you give it " 32 a ",
it will assign 32 to dend and fail to assign a value to dor; scanf()
will then return 1. If you want to assign both values, you need to
check whether scanf() returned 2.
You should pick a better name than "temp1". The simplest thing to do
is to assign the result of scan() to a variable, and test the value
of that variable. Pseudo-code follows:
do {
printf("...\n") ;
items_read = scanf(...);
} while (items_read != 2);
Don't use fflush(stdin). The fflush() function is defined only for
output streams.
scanf() can be very tricky, it can leave unread garbage in your input
stream, and it can be difficult to figure out just what it's doing. A
better approach is to use fgets() to read an entire line, then use
sscanf() to get information from the line you've read.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
moosdau wrote: do you mean the linux os? but I'm using windows,so I can't use the "man" command. could you tell me plz?
Mna pages are also available in hundreds of www servers.
moosdau wrote: do you mean the linux os? but I'm using windows,so I can't use the "man" command. could you tell me plz?
Include context. Without it we have no idea what you mean. See my
sig, and the reference therein, for how to do this.
On Windoze you are probably either using Microsoft VC, or some port
of GCC. With the gcc versions you very likely have info (the
command) available, especially if you have installed DJGPP or
Cygwin. Otherwise the VC help system will probably lead you to a
discussion of scanf.
On this windoze system the command:
C>info libc alpha scanf
brings up a screen that begins as follows:
scanf
=====
Syntax
------
#include <stdio.h>
int scanf(const char *format, ...);
Description
-----------
This function scans formatted text from `stdin' and stores it in
the variables pointed to by the arguments. *Note scanf::.
.... and so forth ...
--
"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
More details at: <http://cfaj.freeshell. org/google/>
Keith Thompson wrote: scanf() returns the number of items assigned. If you give it " 32 a ", it will assign 32 to dend and fail to assign a value to dor; scanf() will then return 1. If you want to assign both values, you need to check whether scanf() returned 2.
You should pick a better name than "temp1". The simplest thing to do
sorry,because I copied it from a long code,
if I named every variant with its meaning, there must be too many
temporary variants.
so ......
is to assign the result of scan() to a variable, and test the value of that variable. Pseudo-code follows:
do { printf("...\n") ; items_read = scanf(...); } while (items_read != 2);
Don't use fflush(stdin). The fflush() function is defined only for output streams.
Thanks very much!!
but I don't know very clearly why shouldn't I use the fflush function.
if there is not any usable data in the input stream,
could I use it to clear the input stream?
if not , could you please tell me why?
scanf() can be very tricky, it can leave unread garbage in your input stream, and it can be difficult to figure out just what it's doing. A better approach is to use fgets() to read an entire line, then use sscanf() to get information from the line you've read.
thanks for your suggestion!
-- Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
moosdau said:
<snip> [...] I don't know very clearly why shouldn't I use the fflush function. if there is not any usable data in the input stream,
Because fflush's behaviour is only defined for streams open for output or
update.
could I use it to clear the input stream?
Not as far as the C language is concerned, no.
if not , could you please tell me why?
For the same reason that pressing the handle on your toilet will not get rid
of the water waiting in your sink's tap (or faucet, if you're on that side
of the pond). Flushing is something we do to output, not to input.
If you want to discard from stdin everything up to and including a
particular character, you can use this function:
#include <stdio.h>
int DiscardFromStre am(FILE *fp, int lim)
{
int ch;
while((ch = getc(fp)) != lim && ch != EOF)
{
continue;
}
return ch == EOF;
}
/* example usage */
int main(void)
{
int ch;
puts("Type a letter, and press ENTER.");
ch = getchar();
if(0 == DiscardFromStre am(stdin, '\n'))
{
printf("You pressed %c\n", ch);
puts("Type another letter, and press ENTER.");
ch = getchar();
if(0 == DiscardFromStre am(stdin, '\n'))
{
printf("You pressed %c\n", ch);
}
}
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
"moosdau" <mo*****@gmail. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. . Thanks very much!! but I don't know very clearly why shouldn't I use the fflush function. if there is not any usable data in the input stream, could I use it to clear the input stream? if not , could you please tell me why?
There's no easy way to purge stdin, esp when it's already potentially empty,
i.e., most [all that I can think of right now] std functions that could
potentially test for this will *wait* if stdin is empty, e.g., getchar,
scanf, gets, fgets, feof, ... it's a toughie, and I would suggest that, *if*
your code need *not* be portable, and *if* your implementation of fflush
works on stdin ok, well, you should perhaps stick with it then! I'll get
flamed for suggesting this of course - or - better still, someone will come
up with a portable way to flush stdin ;-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: CR |
last post by:
having a problem with the final strcpy of filename from *argv into a
structure containing an array with 20 elements called fname
It seems that strcpy & strncpy aren't stopping after null is found but
appending other trash overwriting the array possibly?
Any help would be appreciated!
(if you need all the code let me know)
int main(int argc, char *argv)
{
int *arg_chk;
int i; int j=0;
|
by: Teh Charleh |
last post by:
OK I have 2 similar programmes, why does the first one work and the second does
not? Basically the problem is that the program seems to ignore the gets call if
it comes after a scanf call. Please anything even a hint would be really
helpful, I cant for the life of me see why the 2nd prog wont work...
gets before scanf
code:---------------------------------------------------------------------
|
by: Eric Boutin |
last post by:
Hi ! I was wondering how to quickly and safely use a safe scanf( ) or gets
function... I mean.. if I do :
char a;
scanf("%s", a);
and the user input a 257 char string..
that creates a problem.. same for gets..
even if you create a char array that's 99999999999999 char long.. if the
user input something longer it will still be a bug.. and I don't want
|
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: 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: Lalatendu Das |
last post by:
Dear friends,
I am getting a problem in the code while interacting with a nested
Do-while loop
It is skipping a scanf () function which it should not. I have written
the whole code below. Please help me in finding why such thing is
happening and what the remedy to it is. Kindly bear with my English.
int
main ()
|
by: obdict |
last post by:
Hello,
I used scanf() in a while loop, which ensures that user input is
valid (must be an integer no greater than 21 or less than 3).
If user enters a number out of the range, or enters non-number,
he/she will be asked to retry.
/* start */
int n;
|
by: Neil |
last post by:
Hello
Just to let you know this not homework, I'm learning the language of C
on my own time..
I recently tried to create a escape for user saying
printf ("Do you want to continue? (y or n)");
The scanf statement follows the prompt(printf above) which is the last
statement in the while loop, before the "}"
|
by: tesh.uk |
last post by:
Hi Gurus,
I have written the following code with the help of Ivor Horton's
Beginning C :
// Structures, Arrays of Structures.
#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |