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>
>A handy routine to keep available is flushln, below. For your
application you could call "flushln(stdin) ;". You can also test it
for EOF.

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) || (ch != '\n')) continue;
return ch;
} /* flushln */

for example:

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

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Please don't quote sigs (the text that follows the '-- ' bit) unless you
are specifically commenting on it.
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;
This needs to use the && logical operator to function as Chuck probably
intended it:

while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;

<snip rest>

Jul 13 '08 #21


santosh wrote:
#include <stdio.h>

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) || (ch != '\n')) continue;

This needs to use the && logical operator to function as Chuck probably
intended it:

while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;

<snip rest>
Sorry, I'm new to Groups. And thanks, the code works now!
Jul 13 '08 #22
hdsalbki <hd******@gmail .comwrites:
Thanks to everyone I was able to do this. apart from what vippstar has
sugested I also came with the below code to read the rest of line in
stdin. Is this okay or am I doing something wrong. Thanks for any
comments.

#include <stdio.h>

int main()
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
fflush(stdout);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
}
}
return 0;
}
Why are you checking for '\r' as well as '\n'?

Even if your system happens to represent end-of-line with a CR-LF
pair, that will be translated to a single '\n' character on input (if
you're reading from a text stream, which you are here).

You're also not checking for end-of-file. (Hint: check for EOF, not
feof(stdin).)

I haven't read this whole thread yet, so I don't know whether anyone
else has suggested section 12 of the comp.lang.c FAQ, which covers
stdio. It's at <http://www.c-faq.com>.

--
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 13 '08 #23
hdsalbki <hd******@gmail .comwrites:
santosh wrote:
#include <stdio.h>

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) || (ch != '\n')) continue;

This needs to use the && logical operator to function as Chuck probably
intended it:

while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;

<snip rest>

Sorry, I'm new to Groups. And thanks, the code works now!
It was Chuck's mistake, not yours; the code he posted actually had the
incorrect "||".

Not a huge deal either way; we all make mistakes. But it does
illustrate the fact that it's usually best to copy-and-paste source
code that's actually been successfully compiled and executed rather
than typing it on the fly.

--
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 13 '08 #24
hdsalbki <hd******@gmail .comwrites:
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);
}
}
The program is actually doing just what you told it to do. In
response to the prompt, you typed, say, an 'x'. You didn't see
anything happen, so you pressed the return key (or enter, or however
it's labeled). You typed two characters, 'x' and return, and your
program reported each one.

The issue here is input buffering. (This is distinct from output
buffering, which I discussed in a separate followup.) Though your
scanf call asks for only one character, it doesn't complete until
you've entered a line of text. The new-line character is part of that
line.

More precisely, the scanf call *waits* for a full line of input, but
it only *consumes* a single character. Any other characters in the
line are left waiting in a buffer to be consumed by the next call to
scanf.

Try changing the second printf to:

printf("\nYou typed: '%c'",a);

so the output character is surrounded by single quotes. This will
show you more clearly that it's actually reading and printing each
character you type, including new-line.

Try typing just the return key, with nothing preceding it.

Try typing several characters, such as xyz<return>.

What you'd probably *like* to do is read a single character without
waiting for a complete line. C provides no guaranteed way to do that.
See question 19.1 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Or you can read a single character and then discard the remainder of
the input line. See the rest of this thread for various ways to do
that.

Text input in C is line-oriented. Usually the best way to handle it
is to use fgets() to read a full line at a time into a string, then
use other functions to process the string. You'll get to that later.
(Using fgets() introduces some issues for very long lines; that can
also wait for later.)

--
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 13 '08 #25
hdsalbki <hd******@gmail .comwrites:
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);
}
}
The program is actually doing just what you told it to do. In
response to the prompt, you typed, say, an 'x'. You didn't see
anything happen, so you pressed the return key (or enter, or however
it's labeled). You typed two characters, 'x' and return, and your
program reported each one.

The issue here is input buffering. (This is distinct from output
buffering, which I discussed in a separate followup.) Though your
scanf call asks for only one character, it doesn't complete until
you've entered a line of text. The new-line character is part of that
line.

More precisely, the scanf call *waits* for a full line of input, but
it only *consumes* a single character. Any other characters in the
line are left waiting in a buffer to be consumed by the next call to
scanf.

Try changing the second printf to:

printf("\nYou typed: '%c'",a);

so the output character is surrounded by single quotes. This will
show you more clearly that it's actually reading and printing each
character you type, including new-line.

Try typing just the return key, with nothing preceding it.

Try typing several characters, such as xyz<return>.

What you'd probably *like* to do is read a single character without
waiting for a complete line. C provides no guaranteed way to do that.
See question 19.1 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Or you can read a single character and then discard the remainder of
the input line. See the rest of this thread for various ways to do
that.

Text input in C is line-oriented. Usually the best way to handle it
is to use fgets() to read a full line at a time into a string, then
use other functions to process the string. You'll get to that later.
(Using fgets() introduces some issues for very long lines; that can
also wait for later.)

--
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 13 '08 #26
Keith Thompson <ks***@mib.orgw rites:
hdsalbki <hd******@gmail .comwrites:
>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...
[snip]
The program is actually doing just what you told it to do.
[snip]

Apologies for the double post. I was insufficiently patient with my
news server.

--
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 13 '08 #27
Keith Thompson <ks***@mib.orgw rites:
hdsalbki <hd******@gmail .comwrites:
>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...
[snip]
The program is actually doing just what you told it to do.
[snip]

Apologies for the double post. I was insufficiently patient with my
news server.

--
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 13 '08 #28
vi******@gmail. com writes:
On Jul 13, 4:56 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
<snip>
>The above fails because %*[\n] must match as least one character. The
Don't you mean %*[^\n]? Because that's what is used.
Yes, typo. Sorry. If I have the power to re-write standards I'd
allow a zero-length match for %[^...] since that is often what one
wants.

--
Ben.
Jul 13 '08 #29
"Joachim Schmitz" <no*********@sc hmitz-digital.dewrite s:
<snip>
>Why not
if(scanf("%c%*[^\r\n]%*c", &a) < 1) break;
I've got confused by corrections or corrections so maybe you mean
something else, but this code probably does not do what you expect.

As per my other post, the %*[^\r\n] must match at least one character.
If there is nothing but the \n, the whole scanf stops here having
failed (but it returns 1). The %*c does not get to read the newline.

--
Ben.
Jul 14 '08 #30

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
9599
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,...
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...
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?
1
4330
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
3
3011
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.