473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

echo charecter program...

hi everyone,
I have below a small program to echo back what character the user
types. It's working OK but prints a extra prompt between every
character. what can I do? I'm new in C and my book is very
difficult...
thanks for any help.

#include<stdio. h>

main()
{
char a;
while(1)
{
printf("\nPleas e type a character: ");
scanf("%c",&a);
printf("\nYou typed: %c",a);
}
}

Jul 13 '08
35 1974
hdsalbki wrote:
CBFalconer wrote:
.... snip ...
>
>for example:

do {
/* something that eats chars other than '\n' */
} while (EOF != flushln(stdin)) ;

Thanks CBFalconer. But when I run the following program which uses
your code, I can't get past the first prompt. The app just seems to
there and all I can do is kill it with ctrl-c. Am I doing something
wrong? I copies and pasted your code so to avoid typo...

#include <stdio.h>

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) || (ch != '\n')) continue;
return ch;
} /* flushln */
That || should be a && ------------^ Sorry.
>
int main(void)
{
char a;
while(1)
{
printf("Please enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You typed: %c\n",a);
flushln(stdin);
}
return 0;
}

o/p is:

dodo@sapphire:~/src/c/ws$ ./echo_char3
Please enter a character: x
You typed: x

Program hangs here. whatever I type the prompt is not printing again.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Jul 14 '08 #31
Keith Thompson <ks***@mib.orgw rote:
>
For an unbuffered stream, output appears immediately. For a line
buffered stream, it appears only when a line has been completed.
Not quite -- it is also supposed to appear when the buffer is full
(obviously), when input is requested on an unbuffered stream, and when
input is requested on a line buffered stream whose buffer is empty.
If standard output is going to an interactive device, such as a
terminal or emulator, it's required to be either unbuffered or line
buffered.
Provided the implementation can determine that it *is* an interactive
device. It's not always possible.
If it's line buffered, you need the call to fflush(stdout)
to force a partial line to appear. *However*, on most modern systems,
standard output for an interactive program is likely to be unbuffered,
which means that fflush(stdout) will have no effect.
Actually, most modern system line buffer interactive devices by default
for both input and output, so the above rules make the fflush()
redundant. However:
Adding the
fflush(stdout) is a precaution that may not be necessary for your
system, but it is necessary if you want your program to be maximally
portable.
That is 100% correct!
--
Larry Jones

I'm so disappointed. -- Calvin
Jul 15 '08 #32
In article <1v************ @jones.homeip.n et>,
<la************ @siemens.comwro te:
>Keith Thompson <ks***@mib.orgw rote:
>For an unbuffered stream, output appears immediately. For a line
buffered stream, it appears only when a line has been completed.
>Not quite -- it is also supposed to appear when the buffer is full
(obviously), when input is requested on an unbuffered stream, and when
input is requested on a line buffered stream whose buffer is empty.
Hmmm, traditionally switching from output to input required
an explicit fseek() call; did that change in C99?
--
"There are some ideas so wrong that only a very intelligent person
could believe in them." -- George Orwell
Jul 15 '08 #33
la************@ siemens.com writes:
Keith Thompson <ks***@mib.orgw rote:
>>
For an unbuffered stream, output appears immediately. For a line
buffered stream, it appears only when a line has been completed.

Not quite -- it is also supposed to appear when the buffer is full
(obviously), when input is requested on an unbuffered stream, and when
input is requested on a line buffered stream whose buffer is empty.
Hey, you don't expect me to cover *everything*, do you? 8-)}
>If standard output is going to an interactive device, such as a
terminal or emulator, it's required to be either unbuffered or line
buffered.

Provided the implementation can determine that it *is* an interactive
device. It's not always possible.
But implementations are effectively required to assume that the device
is interactive if they can't prove it isn't. C99 7.19.3p7:

As initially opened, the standard error stream is not fully
buffered; the standard input and standard output streams are fully
buffered if and only if the stream can be determined not to refer
to an interactive device.
>If it's line buffered, you need the call to fflush(stdout)
to force a partial line to appear. *However*, on most modern systems,
standard output for an interactive program is likely to be unbuffered,
which means that fflush(stdout) will have no effect.

Actually, most modern system line buffer interactive devices by default
for both input and output, so the above rules make the fflush()
redundant. However:
Ah, I see. Line-buffering is sufficient because the input request
causes the partial line to appear. Whereas if I write:

printf("Hello, ");
/* something that takes a long time */
printf("world\n ");

the line "Hello, world" will be printed all at once after a delay.
>Adding the
fflush(stdou t) is a precaution that may not be necessary for your
system, but it is necessary if you want your program to be maximally
portable.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 15 '08 #34
Walter Roberson <ro******@ibd.n rc-cnrc.gc.cawrote :
In article <1v************ @jones.homeip.n et>,
<la************ @siemens.comwro te:
Not quite -- it is also supposed to appear when the buffer is full
(obviously), when input is requested on an unbuffered stream, and when
input is requested on a line buffered stream whose buffer is empty.

Hmmm, traditionally switching from output to input required
an explicit fseek() call; did that change in C99?
No, but the above is talking about input on *any* stream, not just the
same stream, so that's not really relevant.
--
Larry Jones

All this was funny until she did the same thing to me. -- Calvin
Jul 17 '08 #35
Keith Thompson <ks***@mib.orgw rote:
>
But implementations are effectively required to assume that the device
is interactive if they can't prove it isn't. C99 7.19.3p7:
Actually, 7.19.5.3p7 is the better reference, since it applies to all
streams, not just stdout and stderr. (I misremembered the requirement
and thought it went the other way.)
Ah, I see. Line-buffering is sufficient because the input request
causes the partial line to appear. Whereas if I write:

printf("Hello, ");
/* something that takes a long time */
printf("world\n ");

the line "Hello, world" will be printed all at once after a delay.
Correct on both counts.
--
Larry Jones

Archaeologists have the most mind-numbing job on the planet. -- Calvin
Jul 17 '08 #36

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

Similar topics

9
6285
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like : Start from the right most charecter of the string to the left of the
3
1222
by: QT | last post by:
Dear sirs, I try to put limit for text field to stop charecter entery after 2 charecter. But I have no success. Any idea how can I do? Best Regards
3
1920
by: ayan4u | last post by:
well i need to deal with white spaces in charecter arrays... with static arrays its fine.. char ss; cin.getline(ss, sizeof ss); .... //deals with white spaces
4
6112
by: situ | last post by:
Hello all, i have a column in a table which stores tag lines which are usually less than 500 charecter. when we display the data through the browser some character display as "boxes" or junks. i predict it may be the newline or carriage return character. is there any function so that i can ward of these characters my database is under utf 8 and my db2level is
2
2023
by: ayan4u | last post by:
ok i have two problems... firstly in strict C enviornment is it possible to have a true dynamic charecter array with no predefined length...i mean to say... suppose.. char *array =NULL; /* actually i dnt know what the exact size of my string is so i aquire sace for 1 charecter from heap /*
2
1167
by: phanimadhav | last post by:
Actually this is my tabledetails Table Name:Organisation Colums:Org_Id,org_Name,Org_Nationality Organisation namess is:Wipro,satyam,CTS,CMC,Infotech,Intel,Syentel,CISCO my requirement is if i enter the one charecter in the textbox.i want to display all releted Organisation names display within LISTBOX.That means firstcharacter of the Org_Name must be match with the with in the textbox character. i am asking number of developers.Some...
2
1866
by: mail2ravi123 | last post by:
hi all, plz provide me a function that is used to compare wild char and as well as to compare * charecter and \ charecter in the string FYI if we want to compare a string for * charecter then we give as \* and add a new slash to it for comparing and for slash also \\ to compare single \ and for wild char just *
4
2505
by: santoshramancha | last post by:
Hi all, what is a conversion charecter and why and where it is used?
0
9720
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
10626
Oralloy
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...
0
10372
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
10374
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
10112
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
9193
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...
1
7650
isladogs
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...
0
6879
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
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.