|
From: <and...@su...> - 2003-04-11 05:34:23
|
Dear programmers,
I´m having to use the fflush command in the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[10];
int a, b, c;
printf("Enter a-->");
scanf("%d", &a);
printf("Enter some name-->");
fflush(stdin);
gets(name);
printf("Enter com b-->");
scanf("%d", &b);
printf("The sum of %d with %d is %d\n", a, b, a+b);
printf("And the name typed was %s\n", name);
system("PAUSE");
return 0;
}
The reason is that if I don´t put it, the program doesn´t wait for the name
to be typed via keyboard. It passes directly to the other operator, b,
indicating that the input buffer is loaded with something. I made some
experiments with short int (hd), but nothing solved.
1-Can anyone explain what is happening?
2-What is the solution for don´t use the fflush?
Regards
Andre
|
|
From: Ricardo R. <ri...@un...> - 2003-04-11 09:38:58
|
scanf ( ) doesnot read the 'return' char,
then it stays in the input buffer until the next reading.
Two options for the next sentence:
(1) If you read in the next sentence with gets( )
the 'return' char is transformed in the
empty char string: " " = '\0' .
(2) If you read a char in the next sentence with scanf( )
the 'return' char it is transformed in the 'line jump'='\n'.
In consequence, if you dont want any strange/side effect in your input
the best option I know after an entry with scanf( ) it's to free the
input buffer:
fflush(stdin).
Any other explanation is wellcome,
but I hope it can help you,
Ricardo.
|
|
From: Per W. <pw...@ia...> - 2003-04-11 10:58:42
|
But fflush() is not portable for input streams! It is only indended for flushing output streams. On a Linux, it would return EOF and set errno to EBADF if the stream isn't opened for output. /Per W On Fri, 11 Apr 2003, Ricardo L=F3pez Ruiz wrote: > scanf ( ) doesnot read the 'return' char, > then it stays in the input buffer until the next reading. > Two options for the next sentence: > (1) If you read in the next sentence with gets( ) > the 'return' char is transformed in the > empty char string: " " =3D '\0' . > (2) If you read a char in the next sentence with scanf( ) > the 'return' char it is transformed in the 'line jump'=3D'\n'. > > In consequence, if you dont want any strange/side effect in your input > the best option I know after an entry with scanf( ) it's to free the > input buffer: > > fflush(stdin). > > > Any other explanation is wellcome, > but I hope it can help you, > Ricardo. > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg= er > for complex code. Debugging C/C++ programs can leave you feeling lost and > disoriented. TotalView can help you find your way. Available on major UNI= X > and Linux platforms. Try it free. www.etnus.com > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@at...> - 2003-04-11 11:13:35
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Andrι Macαrio Barros
> Sent: Friday, April 11, 2003 8:34 AM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] scanf with gets
>
>
> Dear programmers,
>
> I´m having to use the fflush command in the following code:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char name[10];
> int a, b, c;
>
> printf("Enter a-->");
> scanf("%d", &a);
> printf("Enter some name-->");
> fflush(stdin);
The behaviour of the above is undefined (e.g. the airplane may crash).
fflush() is intended to be used on output streams.
You can simply do:
getchar();
> gets(name);
Extremelly Dangerous for buffer overflow (that is you must trust the user
very well, and he user must know the internals of your code). A better one:
fgets(name,10,stdin);
> printf("Enter com b-->");
> scanf("%d", &b);
> printf("The sum of %d with %d is %d\n", a, b, a+b);
> printf("And the name typed was %s\n", name);
> system("PAUSE");
> return 0;
> }
>
> The reason is that if I don´t put it, the program doesn´t
> wait for the name
> to be typed via keyboard. It passes directly to the other operator, b,
> indicating that the input buffer is loaded with something. I made some
> experiments with short int (hd), but nothing solved.
>
> 1-Can anyone explain what is happening?
scanf() leaves the '\n' on the stream. Then gets() was reading and
interpreting it as if you entered an empty string.
> 2-What is the solution for don´t use the fflush?
getchar(); or something. You can even do a function in the style:
void clean_stdin()
{
while(getchar()!='\n')
;
}
And use it. If you are going to use it with stream redirection in the style
myprogram <whatever.txt
for example,
you have to get EOF into account.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Ioannis V. <iv...@at...> - 2003-04-11 19:43:34
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Andr? Macario Barros
> Sent: Friday, April 11, 2003 8:34 AM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] scanf with gets
>=20
>=20
> Dear programmers,
>=20
> I=B4m having to use the fflush command in the following code:
>=20
> #include <stdio.h>
> #include <stdlib.h>
>=20
> int main()
> {
> char name[10];
> int a, b, c;
>=20
> printf("Enter a-->");
> scanf("%d", &a);
> printf("Enter some name-->");
> fflush(stdin);
The behaviour of the above is undefined (e.g. the airplane may crash).
fflush() is intended to be used on output streams.
You can simply do:
getchar();
> gets(name);
Extremelly Dangerous for buffer overflow (that is you must trust the =
user
very well, and he user must know the internals of your code). A better =
one:
fgets(name,10,stdin);
> printf("Enter com b-->");
> scanf("%d", &b);
> printf("The sum of %d with %d is %d\n", a, b, a+b);
> printf("And the name typed was %s\n", name);
> system("PAUSE");
> return 0;
> }
>=20
> The reason is that if I don=B4t put it, the program doesn=B4t=20
> wait for the name
> to be typed via keyboard. It passes directly to the other operator, b,
> indicating that the input buffer is loaded with something. I made some
> experiments with short int (hd), but nothing solved.
>=20
> 1-Can anyone explain what is happening?
scanf() leaves the '\n' on the stream. Then gets() was reading and
interpreting it as if you entered an empty string.
> 2-What is the solution for don=B4t use the fflush?
getchar(); or something. You can even do a function in the style:
void clean_stdin()
{
while(getchar()!=3D'\n')
;
}
And use it. If you are going to use it with stream redirection in the =
style
myprogram <whatever.txt
for example,
you have to get EOF into account.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Ioannis V. <iv...@at...> - 2003-04-11 19:44:01
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Andr? Macario Barros
> Sent: Friday, April 11, 2003 8:34 AM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] scanf with gets
>=20
>=20
> Dear programmers,
>=20
> I=B4m having to use the fflush command in the following code:
>=20
> #include <stdio.h>
> #include <stdlib.h>
>=20
> int main()
> {
> char name[10];
> int a, b, c;
>=20
> printf("Enter a-->");
> scanf("%d", &a);
> printf("Enter some name-->");
> fflush(stdin);
The behaviour of the above is undefined (e.g. the airplane may crash).
fflush() is intended to be used on output streams.
You can simply do:
getchar();
> gets(name);
Extremelly Dangerous for buffer overflow (that is you must trust the =
user
very well, and he user must know the internals of your code). A better =
one:
fgets(name,10,stdin);
> printf("Enter com b-->");
> scanf("%d", &b);
> printf("The sum of %d with %d is %d\n", a, b, a+b);
> printf("And the name typed was %s\n", name);
> system("PAUSE");
> return 0;
> }
>=20
> The reason is that if I don=B4t put it, the program doesn=B4t=20
> wait for the name
> to be typed via keyboard. It passes directly to the other operator, b,
> indicating that the input buffer is loaded with something. I made some
> experiments with short int (hd), but nothing solved.
>=20
> 1-Can anyone explain what is happening?
scanf() leaves the '\n' on the stream. Then gets() was reading and
interpreting it as if you entered an empty string.
> 2-What is the solution for don=B4t use the fflush?
getchar(); or something. You can even do a function in the style:
void clean_stdin()
{
while(getchar()!=3D'\n')
;
}
And use it. If you are going to use it with stream redirection in the =
style
myprogram <whatever.txt
for example,
you have to get EOF into account.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|