Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Doubt about getchar() and scanf() for a character?

Reply
Thread Tools

Doubt about getchar() and scanf() for a character?

 
 
Vinicius
Guest
Posts: n/a
 
      10-12-2004
Hello,

the following code does not work:
"
....
void main(void)
{
char option;

printf("Choose an option: ");

option = toupper(getchar());

printf("Chosse another option: ");

scanf("%c", &option);

...
"

It shows "Choose an option: Choose another option ". Why?
Whether I put two lines "fflush(stdin)" before the inputs, the same
above occurs.

my gcc: "$ gcc --version
gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

TIA, Vinicius.
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      10-12-2004
Vinicius wrote:
> Hello,
>
> the following code does not work:
> "
> ...
> void main(void)

Yep, this could be one reason.
The main() function returns int. Always.
Anything else may lead to undefined behavior.


> {
> char option;
>
> printf("Choose an option: ");
>
> option = toupper(getchar());
>
> printf("Chosse another option: ");
>
> scanf("%c", &option);
>
> ...
> "
>
> It shows "Choose an option: Choose another option ". Why?

Because you typed in the code instead of pasting it.
I show:
Choose an option: Chosse another option:
This depends on your operating system and how it handles
input and output to the user. Read the C language FAQ
below for more information.


> Whether I put two lines "fflush(stdin)" before the inputs, the same
> above occurs.

The fflush() is not defined for input streams. Again,
another FAQ. You could try flushing the output buffer,
echoing your input character or other platform dependent
solutions. Again, read the FAQ.


>
> my gcc: "$ gcc --version
> gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
>
> TIA, Vinicius.



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
Minti
Guest
Posts: n/a
 
      10-12-2004


"Vinicius" <> wrote in message
news: m...
> Hello,
>
> the following code does not work:
> "
> ...
> void main(void)
> {
> char option;
>
> printf("Choose an option: ");
>
> option = toupper(getchar());
>
> printf("Chosse another option: ");
>
> scanf("%c", &option);
>
> ...
> "
>
> It shows "Choose an option: Choose another option ". Why?
> Whether I put two lines "fflush(stdin)" before the inputs, the same
> above occurs.
>
> my gcc: "$ gcc --version
> gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
>
> TIA, Vinicius.


Please read the FAQ { http://www.eskimo.com/~scs/c-faq/top.html }, and
specifically read about, topics on

void main()
fflush(stdin)


If you are careful, you will also find an answer to your problem.I am too
lazy to find a solution to your problem.

Tip: It's nothing to do with the version of your compiler.


--
Imanpreet Singh Arora
Zmoc.Zliamg@Zteerpnami
Remove Z to mail
"Things may come to those who wait, but only the things left by those who
hustle."
Abraham Lincoln














 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      10-13-2004
Vinicius <> wrote:

> the following code does not work:


What exactly does not work? I think it works as designed, but perhaps
not as expected by you.
Have you tried to print out `option' after each read?

> "
> ...
> void main(void)

int main(void);
> {
> char option;


> printf("Choose an option: ");
>
> option = toupper(getchar());


> printf("Chosse another option: ");


> scanf("%c", &option);


> ...
> "


> It shows "Choose an option: Choose another option ". Why?


On my system the two messages are on two different lines,
unless you run them with stdin redirected, like this:
./a.out <In

> Whether I put two lines "fflush(stdin)" before the inputs, the same
> above occurs.


Don't do it, it's undefined. What possibly could it do?


If stdin is your terminal, then getchar() will read the first character
entered, and scanf() will read the next character that is in the *stream*,
which is possibly a carriage-return - depending on how you enter the
characters:
a<CR>b<CR> - first `option' is 'a', second time it is '\n'
ab<CR> - first it is 'a', then it is 'b'
a b<CR> - first read 'a', then ' '
a<CR>b<CR> - first read ' ', then 'a'
etc...

If the scanf line looked like this:
scanf(" %c", &option); /* note the extra space */
then scanf would skip any white-space characters before reading
next character:
a<CR>b<CR> - read 'a' (getchar()), then skip '\n' and read 'b' (scanf())
a<CR> <TAB>b<CR> - after reading 'a', '\n', ' ' and '\t' are ignored
and 'b' gets into `option'
a<CR>b<CR> - getchar() reads first ' ', scanf() skips the second ' '
and reads 'a'

Note, on top of all above, the terminals are line-buffered, so you can't
enter any characters without entering (pressing) <CR>. The situation
is a little different when your stdin is a file.

scanf() is a useful function, but not easy to understand, and if you're
a beginner, leave it for later. Better use fgets() for reading; atoi()
and strtol() families for conversions, but maybe sscanf() would prove
easy to use here as well.

To know scanf() you have to _carefully_ read its description. It is
not an "inverse" of printf(), and the format string similarities with
that of printf() are only apparent.

--
Stan Tobias
sed 's/[A-Z]//g' to email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
Re: Thank you, Silvio and Sudsy[Was: A doubt about servlet and http..] Sudsy Java 0 04-14-2006 11:00 PM
doubt in FLI Program and order of execution priya VHDL 0 10-03-2005 12:55 PM
doubt regarding mcsa messaging and mcse security =?Utf-8?B?VGFuZ28=?= MCSE 1 09-04-2005 08:20 PM



Advertisments