473,806 Members | 2,707 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 1973


vipps...@gmail. com wrote:
On Jul 13, 2:58 pm, hdsalbki <hdsal...@gmail .comwrote:
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!

#include <stdio.h>

int main()
I told you int main(void), not int main().
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c",&a) < 1)
{
break;
}
printf("You typed: %c\n",a);
fflush(stdout);
That fflush() is not necessary, as you write a newline to stdout. That
will flush the stream, since it's line buffered. (_IOLBF in setvbuf)
}
return(0);
return is not a function, it's a keyword. the parenthesis are not
needed, just like sizeof. (do not confuse it with sizeof (cast), it's
the cast that requires parenthesis, not sizeof)

}

o/p is:

dodo@sapphire:~/src/c/ws$ ./echo_char1

Please enter a character: a
You typed: a

Please enter a character: b
You typed:

Please enter a character: c
You typed: c

Please enter a character: d
You typed:

Please enter a character:
Yeah, it seems that it does not actually read the newline with %*c.
I can't tell you why this happends, perhaps someone else can explain.
Remove the %*c and add a getchar(), and it should work:

if(scanf("%c%^[\n]", &a) < 1) break;
(void)getchar() ;

Note that the cast to (void) is not necessary; I only added it to make
more clear that you don't need to check the return value of getchar.
Thank you vippstar. I will use int main(void) from now on and use
fflush(stdout) only when there is no \n after output. Okay I confused
the return keyword with if() and while() which need brackets. And your
change works! looking at info for getchar() it says it is for reading
single character...may be I should've done it with getchar()...tha nks
again.
Jul 13 '08 #11
On Jul 13, 4:35 pm, hdsalbki <hdsal...@gmail .comwrote:
hdsalbki wrote:
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.
<snip wrong code>
>
Sorry some errors were there. Corrected code follows:
<snip wrong code>

Both are wrong. My code is correct, compare both codes and try to
understand why yours is not correct.
Do not continue this discussion if you're not a troll. Just reread the
discussion and use a reference for all the functions you will use.
Jul 13 '08 #12
hdsalbki wrote:
hdsalbki wrote:
>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;
}

Sorry some errors were there. Corrected code follows:

#include <stdio.h>

int main(void)
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
Why not
if(scanf("%c%*[^\r\n]%*c", &a) < 1) break;
}
}
return 0;
}
Bye, Jojo
Jul 13 '08 #13
Joachim Schmitz wrote:
hdsalbki wrote:
>hdsalbki wrote:
>>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;
}

Sorry some errors were there. Corrected code follows:

#include <stdio.h>

int main(void)
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
Why not
if(scanf("%c%*[^\r\n]%*c", &a) < 1) break;
Oops. Should be in the outer while, the inner while deleted entirely.
>
> }
}
return 0;
}
Bye, Jojo
Jul 13 '08 #14
hdsalbki <hd******@gmail .comwrites:
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!

#include <stdio.h>

int main()
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c",&a) < 1)
{
break;
}
printf("You typed: %c\n",a);
fflush(stdout);
}
return(0);
}
You may have thought you picked an easy example, but this is very hard
using scanf. I simply would not try it. All the suggestions so far
are wrong in subtle ways.

The above fails because %*[\n] must match as least one character. The
match fails at that point (if there is nothing before the \n) and the
whole scanf return without doing the %*c (read without assigning a
char).

I'd use a getc call and a look to read and discard any remaining
characters up to the newline.

--
Ben.
Jul 13 '08 #15
On Jul 13, 4:56 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
hdsalbki <hdsal...@gmail .comwrites:
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!
<snip code>
>
You may have thought you picked an easy example, but this is very hard
using scanf. I simply would not try it. All the suggestions so far
are wrong in subtle ways.
My code along with the getchar() fix is fine.
The above fails because %*[\n] must match as least one character. The
Don't you mean %*[^\n]? Because that's what is used.
match fails at that point (if there is nothing before the \n) and the
whole scanf return without doing the %*c (read without assigning a
char).
Ah, so that's why that happends. Thanks.
<snip>
Jul 13 '08 #16


Ben Bacarisse wrote:
hdsalbki <hd******@gmail .comwrites:
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!

#include <stdio.h>

int main()
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c",&a) < 1)
{
break;
}
printf("You typed: %c\n",a);
fflush(stdout);
}
return(0);
}

You may have thought you picked an easy example, but this is very hard
using scanf. I simply would not try it. All the suggestions so far
are wrong in subtle ways.

The above fails because %*[\n] must match as least one character. The
match fails at that point (if there is nothing before the \n) and the
whole scanf return without doing the %*c (read without assigning a
char).

I'd use a getc call and a look to read and discard any remaining
characters up to the newline.

--
Ben.
Thanks Ben. I see now that getc() is suited for this. I used scanf
because I have not yet arrived in my book where getchar()/getc() is
used.
Jul 13 '08 #17


vipps...@gmail. com wrote:
On Jul 13, 4:35 pm, hdsalbki <hdsal...@gmail .comwrote:
hdsalbki wrote:
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.
<snip wrong code>

Sorry some errors were there. Corrected code follows:
<snip wrong code>

Both are wrong. My code is correct, compare both codes and try to
understand why yours is not correct.
Do not continue this discussion if you're not a troll. Just reread the
discussion and use a reference for all the functions you will use.
Sorry if I offended you. I thought my version was correct because it
behaved just like your code. Anyway thanks for your help...i'm moving
on to the next exercise!
Jul 13 '08 #18
hdsalbki wrote:
vipps...@gmail. com wrote:
.... snip ...
>
>Note that the cast to (void) is not necessary; I only added it
to make more clear that you don't need to check the return value
of getchar.

Thank you vippstar. I will use int main(void) from now on and use
fflush(stdout) only when there is no \n after output. Okay I
confused the return keyword with if() and while() which need
brackets. And your change works! looking at info for getchar() it
says it is for reading single character...may be I should've done
it with getchar()...tha nks again.
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.
Jul 13 '08 #19


CBFalconer wrote:
hdsalbki wrote:
vipps...@gmail. com wrote:
... snip ...
Note that the cast to (void) is not necessary; I only added it
to make more clear that you don't need to check the return value
of getchar.
Thank you vippstar. I will use int main(void) from now on and use
fflush(stdout) only when there is no \n after output. Okay I
confused the return keyword with if() and while() which need
brackets. And your change works! looking at info for getchar() it
says it is for reading single character...may be I should've done
it with getchar()...tha nks again.

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.
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 */

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.
Jul 13 '08 #20

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
1865
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
2504
by: santoshramancha | last post by:
Hi all, what is a conversion charecter and why and where it is used?
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,...
0
10624
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
10371
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...
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
6877
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
5546
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...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.