473,803 Members | 2,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(std in)" 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.
Nov 14 '05 #1
3 2760
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(std in)" 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.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #2


"Vinicius" <cv********@cli ck21.com.br> wrote in message
news:87******** *************** **@posting.goog le.com...
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(std in)" 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@Zte erpnami
Remove Z to mail
"Things may come to those who wait, but only the things left by those who
hustle."
Abraham Lincoln




Nov 14 '05 #3
Vinicius <cv********@cli ck21.com.br> 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(std in)" 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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
639
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets (data,5,stdin); the_number = atoi (data);
11
28281
by: Mars | last post by:
char c; int i; scanf("%d",&i); c=getchar(); I want to read a integer and a character in 2 lines. Why the getchar always get the \n character from the last line????? (Sorry for my newbie question, but where is the FAQ section of the
19
3730
by: mailursubbu | last post by:
HI, Below is my program. I compiled it through g++. Now strange thing is, getc is not reading the data instead its printing the previously read data ! Please some one let me know whats wrong. #include<stdio.h> int main() { int a;
1
3723
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if (!isspace(input)) j++;
6
5281
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
25
5452
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
26
4545
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
0
9703
marktang
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...
0
10317
jinu1996
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...
1
10300
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,...
0
10069
tracyyun
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...
0
9127
agi2029
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...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.